使用django2.0.2 ptyhon3.4
MySerializer.py
class post(serializers.ModelSerializer)
class Meta:
model = post
fields = ("postuid","useruid","content")
class reply(serializers.ModelSerializer)
class Meta:
model = reply
fields = ("replyuid","postuid","useruid","content")
class postview(serializers.ModelSerializer)
reply = reply()
post = post()
fields = ('reply','post')???
我想要这样的表格
{"postview":{"post":{postfield},"reply":[{replyfield}]}
如何做到这一点
postview不是modelserializer?
答案 0 :(得分:1)
您可以改为使用serializers.Serializer
课程:
class postview(serializers.Serializer):
reply = reply(many=True)
post = post()
注意Serializer
基类,您不需要Meta
个选项。只需添加所需的字段作为序列化程序的属性。
您可以使用many=True
属性指定reply
包含对象列表。