DRF 多对多序列化和反序列化
一、 序列化
-
查询所有的学生的信息,包括任课老师
-
models.py
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='teacher')
-
serializer.py
from rest_framework import serializers from . import models
# 老师序列化 class TeacherSerializer(serializers.ModelSerializer): class Meta: model = models.Teacher fields = ('id','name') # 学生序列化 class StudentSerializer(serializers.Serializer): name = serializers.CharField(max_length=32) teacher = TeacherSerializer(many=True,read_only=True)
-
views.py
# 查询所有的学生对应的老师
studentobj = models.Student.objects.all() stu = StudentSerializer(studentobj,many=True) return Response(stu.data)
-
结果
[ { "name": "小张", "teacher": [ { "id": 3, "name": "王老师" } ] }, { "name": "小王", "teacher": [ { "id": 4, "name": "付老师" } ] }, { "name": "小赵", "teacher": [] }, { "name": "小琪", "teacher": [] } ]
-
-
查询所有的老师的信息,包括所教学生
-
models.py
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='teacher')
-
serializer.py
from rest_framework import serializers from . import models
# 学生序列化
class StudentSerializer(serializers.ModelSerializer): class Meta: model = models.Teacher fields = ('id','name')
# 老师序列化
class TeacherSerializer(serializers.Serializer): name = serializers.CharField(max_length=32) students = StudentSerializer(many=True,read_only=True)
-
views.py
teacherobj = models.Teacher.objects.all() teach = TeacherSerializer1(teacherobj,many=True) return Response(teach.data)
-
结果
[ { "name": "王老师", "students": [] }, { "name": "刘老师", "students": [] }, { "name": "郑老师", "students": [ { "id": 1, "name": "小张" } ] }, { "name": "董老师", "students": [ { "id": 2, "name": "小李" } ] } ]
-
二、反序列化
-
老师添加学生
-
models.py
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='teacher')
-
serializer.py
# 反序列化
class TeacherUnserializer(serializers.Serializer): id = serializers.IntegerField() student_id = serializers.ListField() def create(self,data): tid = data.pop('id') student_id = data.pop('student_id') teacherobj = models.Teacher.objects.filter(id=tid).first() teacherobj.students.set( student_id ) return teacherobj
-
views.py
data = { "id":2, "student_id":[1,2,3,4] } obj = TeacherUnserializer(data=data) if obj.is_valid(): obj.save() print(obj.errors) return Response('ok')
-
-
学生添加老师
-
models.py
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='teacher')
-
serializer.py
# 反序列化
class StudentUnserializer(serializers.Serializer): id = serializers.IntegerField() teacher_id = serializers.ListField() def create(self,data): id = data.pop('id') teacher_id = data.pop('teacher_id') studentobj = models.Student.objects.filter(id=id).first() studentobj.teacher.set( teacher_id ) return studentobj
-
views.py
data = { "id": 2, "teacher_id": [1, 2, 3, 4] } obj = StudentUnserializer(data=data) if obj.is_valid(): obj.save() print(obj.errors) return Response('ok')
-