django modelserializer合并

时间:2018-05-17 04:54:18

标签: django django-models django-serializer

使用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?

1 个答案:

答案 0 :(得分:1)

您可以改为使用serializers.Serializer课程:

class postview(serializers.Serializer):
    reply = reply(many=True)
    post  = post()

注意Serializer基类,您不需要Meta个选项。只需添加所需的字段作为序列化程序的属性。

您可以使用many=True属性指定reply包含对象列表。