class ChatSerializer(serializers.ModelSerializer):
creator = UserSerializer()
class Meta:
model = Affairs
fields = ('creator', 'message', 'date')
如何隐藏模型字段?我想得到相同的结果,但是在JSON中有其他键。
答案 0 :(得分:0)
如文档所述,您可以定义字段成员var以指定要包含的字段,也可以定义排除成员var以指定不想要的字段
class ChatSerializer(serializers.ModelSerializer):
...
class Meta:
model = Affairs
exclude = ('secret', 'hidden', 'waldo')
https://www.django-rest-framework.org/api-guide/serializers/#specifying-which-fields-to-include