zoukankan      html  css  js  c++  java
  • Django多对多的增删改查

    Django 多对多 关系

    • 多对多,本意就是多个一对多的关系

    • 定义多对多 ManyToManyField 字段

      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')
      
    • 多对多添加

      • 添加一条记录
      # 老师选择学生
      teacherobj = models.Teacher.objects.filter(id=1).first()
      studentobj = models.Student.objects.filter(id=1).first()
      teacherobj.students.add(studentobj)
      return Response('ok')
      
      # 学生选择老师
      studentobj = models.Student.objects.filter(id=1).first()
      teacherobj = models.Teacher.objects.filter(id=2).first()
      studentobj.stu.add(teacherobj)
      return Response('ok')
      
      • 添加多条记录
      # 老师选择多名学生
      teacherobj = models.Teacher.objects.filter(id=3).first()
      teacherobj.students.add(*[1,2,3,4])
      teacherobj.save()
      
      # 学生选择多名老师
      studentobj = models.Student.objects.filter(id=2).first()
      studentobj.stu.add(*[3,4])
      studentobj.save()
      
      • 添加不重复的记录
      # 老师选择多名学生
      teacherobj = models.Teacher.objects.filter(id=3).first()
      teacherobj.students.set(*[1,2,3,4])
      teacherobj.save()
      
      # 学生选择多名老师
      studentobj = models.Student.objects.filter(id=2).first()
      studentobj.stu.set(*[3,4])
      studentobj.save()
      
    • 多对多查询

      # 查询老师有多少名学生
      teacherobj = models.Teacher.objects.filter(id=3).first()
      stuobj = teacherobj.students.all()
      
      # 查询学生有多名老师
      studentobj = models.Student.objects.filter(id=2).first()
      teacherobj = studentobj.stu.all()
      
    • 多对多更新

      # 老师批量换学生
      teacherobj = models.Teacher.objects.filter(id=3).first()
      teacherobj.students.set([1,2,3,4])
      
      # 学生批量换老师
      studentobj = models.Student.objects.filter(id=3).first()
      studentobj.stu.set([1,2,3,4])
      
    • 多对多删除

      # 老师删除一个学生
      teacherobj = models.Teacher.objects.filter(id=3).first()
      studentobj = models.Student.objects.filter(id=2).first()
      teacherobj.students.remove(studentobj)
      
      # 学生删除一个老师
      studentobj = models.Student.objects.filter(id=3).first()
      teacherobj = models.Teacher.objects.filter(id=4).first()
      studentobj.stu.remove(teacherobj)
      studentobj.save()
      
      # 老师删除所有学生
      teacherobj = models.Teacher.objects.filter(id=1).first()
      teacherobj.students.clear()
      teacherobj.save()
      
      # 学生删除所有老师
      studentobj = models.Student.objects.filter(id=3).first()
      studentobj.stu.clear()
      studentobj.save()
      
    • 基于python3.6 Django 2.1.4

  • 相关阅读:
    01 drf源码剖析之restful规范
    08 Flask源码剖析之flask拓展点
    07 flask源码剖析之用户请求过来流程
    06 flask源码剖析之路由加载
    05 flask源码剖析之配置加载
    04 flask源码剖析之LocalStack和Local对象实现栈的管理
    03 flask源码剖析之threading.local和高级
    02 flask源码剖析之flask快速使用
    01 flask源码剖析之werkzurg 了解wsgi
    MVC之Filter
  • 原文地址:https://www.cnblogs.com/wuxiaoshi/p/11831268.html
Copyright © 2011-2022 走看看