【发布时间】:2022-01-05 14:17:23
【问题描述】:
我正在使用带有 django-simple-history 的 Django Rest Framework,目前我想在我的 Board rest API 中返回历史修改,目前它运行良好,但我想隐藏一些字段。这是当前的输出:
但是,我不需要id、history_id 等。
我的实现与post 中的 alexander 答案相同。 这是我目前的序列化程序,我将历史记录放在我的 Board 模型上
class HistoricalRecordField(serializers.ListField):
child = serializers.DictField()
def to_representation(self, data):
representation = super().to_representation(data.values())
# i've tried to do it by deleting, but does't work well.
del representation[0]['history_id']
return representation
class BoardSerializer(serializers.ModelSerializer):
history = HistoricalRecordField(read_only=True)
class Meta:
model = Board
fields = '__all__'
但这似乎不是最好的方法。
如果您对如何以正确的方式进行操作有一些提示,我想知道。 提前致谢!
【问题讨论】:
-
你能分享一下你的序列化器的全部代码吗?
-
@ThomasGth 已更新
标签: django django-rest-framework django-simple-history