【发布时间】:2016-12-19 22:41:18
【问题描述】:
在下面的代码中,模板是一个外键。模板是嵌套序列化程序以询问序列化程序。
[{
"pk": 15,
"template": {
"question_type": 1,
"question": "What is your age ?",
"answer_type": 1,
"available_choices": []
},
"order": 1,
"mandatory": true
}]
我要展示的是:
[
{
"pk": 15,
"question_type": 1,
"question": "What is your age?",
"order": 1,
"answer_type": 1,
"mandatory": true,
"available_choices": []
}]
如何显示这样的嵌套序列化程序数据字段?
这是我的序列化程序类:
class TemplateSerializer(serializers.ModelSerializer):
available_choices = ChoiceSerializer(many=True)
class Meta:
model = Template
fields = (
'question_type', 'question', 'answer_type',
'available_choices'
)
class ASerializer(serializers.ModelSerializer):
template = TemplateSerializer()
class Meta:
model = A
fields = (
'pk', 'template', 'order', 'mandatory'
)
【问题讨论】:
-
编写自定义序列化程序!
标签: django django-rest-framework