多对多修改

  • 模型

    from django.db import models
    
    # 学生类
    class Student(models.Model):
        name = models.CharField(max_length=32)
    # 老师类
    class Teacher(models.Model):
        name = models.CharField(max_length=32)
        students = models.ManyToManyField(to='Student',related_name='stu')
    
  • 视图

    • 老师换学生

      from rest_framework.views import APIView
      from rest_framework.response import Response
      from . import models
      # 老师类
      class TeacherView(APIView):
          def delete(self, request):
              teacherobj = models.Teacher.objects.filter(id=3).first()
              teacherobj.students.set(*[1,2,3,4])
              return Response({
                  "status": 200,
                  "msg":"编辑成功"
              })
      
    • 学生换老师

      from rest_framework.views import APIView
      from rest_framework.response import Response
      from . import models
      # 学生类
      class StudentView(APIView):
          def delete(self, request):
              stuobj = models.Student.objects.filter(id=2).first()
              stuobj.stu.set(*[1,2,3,4])
              return Response({
                  "status": 200,
                  "msg":"编辑成功"
              })
      

results matching ""

    No results matching ""