首页 > 解决方案 > 重命名嵌套序列化程序中的字段 - Django Rest Framework

问题描述

我正在尝试重命名序列化程序中的字段,如下所示:

class ExampleTwoSerializer(serializers.ModelSerializer):

    class Meta:
        model = ExampleTwo
        exclude = ("some_field", "another_field")


class ExampleOneSerializer(CustomModelSerializer):
    new_fied = ExampleOneSerializer(source="example_model_field", many=True)

    class Meta:
        model = ExampleOne
        exclude = ("some_field", "another_field")

但我得到这个错误:

AttributeError at /api/example/
Got AttributeError when attempting to get a value for field `new_field` on serializer `ExampleTwoSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `ExampleTwo` instance.
Original exception text was: 'ExampleTwo' object has no attribute 'new_fied'.

标签: django-rest-framework

解决方案


推荐阅读