具有多对多关系的DRF 2路嵌套序列化

问题描述 投票:0回答:1

我使用中间模型在我的[[学生和模型之间具有多对多关系

class Person(models.Model): # cf/NIN optional by design cf = models.CharField(_('NIN'), unique=True, blank=True, null=True, max_length=16) first_name = models.CharField(_('first name'), blank=False, max_length=40) last_name = models.CharField(_('last name'), blank=False, max_length=40) date_of_birth = models.DateField(_('date of birth'), blank=False) class Meta: ordering = ['last_name', 'first_name'] abstract = True def __str__(self): return self.first_name + ' ' + self.last_name class Macro(models.Model): name = models.CharField(_('name'), unique=True, blank=False, max_length=100) description = models.TextField(_('description'), blank=True, null=True) class Meta: ordering = ['name'] def __str__(self): return self.name class Student(Person): enrollment_date = models.DateField(_('enrollment date'), blank=True, null=True) description = models.TextField(_('description'), blank=True, null=True) macro = models.ManyToManyField(Macro, through='MacroAssignement') class MacroAssignement(models.Model): student = models.ForeignKey(Student, related_name='macros', on_delete=models.CASCADE) macro = models.ForeignKey(Macro, related_name='students', on_delete=models.CASCADE) def __str__(self): return str(self.student)
我配置序列化程序以便在对学生进行序列化时利用嵌套序列化

class PersonSerializer(serializers.ModelSerializer): class Meta: model = Person fields = ('id', 'cf', 'first_name', 'last_name', 'date_of_birth') abstract = True class StudentSerializer(PersonSerializer): macro = serializers.StringRelatedField(many=True, read_only=True) class Meta(PersonSerializer.Meta): model = Student fields = PersonSerializer.Meta.fields + ('enrollment_date', 'description', 'macro') extra_kwargs = {'enrollment_date': {'default': date.today()}, 'description': {'required': False}} class MacroSerializer(serializers.ModelSerializer): students = StudentSerializer(many=True, read_only=True) class Meta: model = Macro fields = ('id', 'name', 'description', 'students')

直到这里,没有问题,当我请求

student

数据时,macro相关信息随之出现。这是一个例子 { "id": 18, "cf": "ciaciacia", "first_name": "Paolo", "last_name": "Bianchi", "date_of_birth": "2020-05-01", "enrollment_date": null, "description": null, "macro": [ "macro1" ] },
现在相反,当我请求

宏]时,我也想查看相关的学生列表。我也尝试在MacroSerializer中实现嵌套序列化class MacroSerializer(serializers.ModelSerializer): students = StudentSerializer(many=True, read_only=True)

这不起作用,因为出现以下错误

AttributeError: Got AttributeError when attempting to get a value for field `first_name` on serializer `StudentSerializer`. The serializer field might be named incorrectly and not match any attribute or key on the `MacroAssignement` instance. Original exception text was: 'MacroAssignement' object has no attribute 'first_name'.

[注意:first_name是从[[Person
模型继承的

Student模型的字段]当然,我可以实现查询数据库并获取分配给给定宏的学生姓名的功能,但是我想知道是否有内置的Django方法。有点像[[2-way嵌套序列化

我使用中间模型类Person(models.Model)在我的Student模型和Macro模型之间具有多对多关系:#cf / NIN设计为可选cf = models.CharField(_('NIN'), ...
django django-models django-rest-framework django-serializer django-database
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.