我有两个序列化器,其中一个序列化器通过关系引用另一个序列化器many=True。
class AttributeInParentSerializer(ModelSerializer):
masterdata_type = CharField(max_length=256, source='masterdata_type_id')
class Meta:
model = Attribute
fields = ('uuid', 'masterdata_type')
class ArticleInArticleSetSerializer(ModelSerializer):
attributes = AttributeInParentSerializer(many=True)
class Meta:
model = Article
fields = ('uuid', 'attributes')
Run Code Online (Sandbox Code Playgroud)
Article 中属性的顺序并不总是相同,但我想以相同的顺序输出它们,因此在本例中对 field 进行排序masterdata_type。我怎样才能做到这一点?请注意,如果可能的话,我不想更改序列化器的任何客户端,当然也不想更改任何模型。