django-rest-framework - 重命名嵌套序列化程序中的字段 - 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'.
解决方案
推荐阅读
- laravel - Laravel - 从另一个 sql 表中过滤结果
- sql - SQL Server 克隆树表
- android - 使用自定义 UncaughtExceptionHandler 升级 Firebase Crashlytics
- amazon-web-services - 我如何使用他们的 aws sdk 从 aws s3 存储桶中按 id 获取一个对象文件?
- r - R 子集和求和而不诉诸 for 循环
- javascript - useState react hook不设置数组状态
- jpa - 使用spring data jpa获取自定义字段
- django - Django TypeError - 'ModelBase' 对象不可迭代
- c++ - 是否可以在 C++ 应用程序中自定义 rapidxml 使用换行符和制表符?
- bokeh - Bokeh 表中的条件 DateFormatter