只需使用相关字段而不进行设置
many=True。
请注意,这也是因为您想要将输出命名为
category_name,而实际字段是
category,因此需要
source在序列化程序字段上使用参数。
以下应该为您提供所需的输出…
class ItemSerializer(serializers.ModelSerializer): category_name = serializers.RelatedField(source='category', read_only=True) class meta: model = Item fields = ('id', 'name', 'category_name')


