如何使用模型序列化器在 django-rest 中序列化一对多关系?
Posted
技术标签:
【中文标题】如何使用模型序列化器在 django-rest 中序列化一对多关系?【英文标题】:How to serialize a one to many relation in django-rest using Model serializer? 【发布时间】:2017-02-13 01:56:29 【问题描述】:这些是我的模型和序列化程序。我想要问题模型的表示以及被问到问题的人员列表。
我正在尝试这个:
@api_view(['GET', 'PATCH'])
def questions_by_id(request,user,pk):
question = Question.objects.get(pk=pk)
if request.method == 'GET':
serializer = QuestionSerializer(question)
return Response(serializer.data)
但我得到一个空字典 ()。但是,当我从
QuestionSerializer
中删除asked
字段时,我得到了Question
的完整表示以及Places
很好地序列化。我错过了什么?
class AskedToSerializer(serializers.ModelSerializer):
class Meta:
model = AskedTo
fields = ('to_user', 'answered')
class QuestionSerializer(serializers.ModelSerializer):
class Meta:
model = Question
places = PlaceSerializer(many=True, required=False)
asked = AskedToSerializer(source='askedto_set', many=True)
fields = ('id', 'created_on', 'title', 'places', 'answered','asked')
extra_kwargs = 'created_by': 'read_only': True
class Question(BaseModel):
title = models.CharField(max_length=200, null=False)
places = models.ManyToManyField(Place, blank=True)
answered = models.BooleanField(default=False)
class AskedTo(BaseModel):
ques = models.ForeignKey(Question, on_delete=models.CASCADE)
to_user = models.ForeignKey(settings.AUTH_USER_MODEL)
replied = models.BooleanField(default=False)
class Place(models.Model):
g_place_id = models.CharField(max_length=20,primary_key=True)
json = models.TextField(null=True)
name = models.CharField(max_length=40)
【问题讨论】:
【参考方案1】:我想通了。有两个错误。
改变了这个:
class AskedToSerializer(serializers.ModelSerializer):
class Meta:
model = AskedTo
fields = ('to_user', 'answered')
对此(注意字段的变化,模型和序列化器上的字段不匹配)
class AskedToSerializer(serializers.ModelSerializer):
class Meta:
model = AskedTo
fields = ('to_user', 'replied')
其次,我需要在class Meta
之外定义任何额外的字段
class QuestionSerializer(serializers.ModelSerializer):
places = PlaceSerializer(many=True, required=False)
asked = AskedToSerializer(source='askedto_set', many=True)
class Meta:
model = Question
fields = ('id', 'created_on', 'title', 'places', 'answered','asked')
extra_kwargs = 'created_by': 'read_only': True
注意places
和asked
定义的变化。
【讨论】:
【参考方案2】:就我而言,我有这个 models.py:
class Section(models.Model):
title = models.CharField(max_length=500)
description = models.TextField(blank=True)
user = models.ForeignKey(Profile, related_name="sections", on_delete=models.CASCADE)
class Feed(models.Model):
title = models.CharField(max_length=500)
link_rss = models.URLField(max_length=500)
link_web = models.URLField(max_length=500)
description = models.TextField(blank=True)
language = models.CharField(max_length=50, blank=True)
logo = models.URLField(blank=True)
sections = models.ManyToManyField(Section, related_name="feeds")
我完成了 serializers.py 这个:
class FeedSerializer(serializers.ModelSerializer):
sections = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Feed
fields = '__all__'
class SectionSerializer(serializers.ModelSerializer):
feeds = FeedSerializer(many=True, read_only=True)
class Meta:
model = Section
exclude = ('user',)
【讨论】:
以上是关于如何使用模型序列化器在 django-rest 中序列化一对多关系?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用外部回归器在 BigQuery 中训练 Arima_PLUS 模型?
如何使用 Autodesk Forge 查看器在 3D 模型中查找位置(x、y、z 坐标)
django-rest framework 接口规范 原生django接口单查群查 postman工具 CBV源码解析
python django-rest-framework 3.3.3 更新嵌套序列化程序