【发布时间】:2018-08-03 15:59:07
【问题描述】:
我有一个序列化器和一个用于通用外键关系的相关字段
这应该用于序列化 content_object 这是一个 ContentType 实例。我需要检查我在相关字段中序列化的Notification 对象的type,以正确知道哪些附加字段要序列化到其中的data 参数中。实现这一目标的最佳方法是什么?
class NotificationRelatedField(serializers.RelatedField):
def to_representation(self, value):
data = {}
# Need to check notification 'type' here
return data
class NotificationRetrieveSerializer(serializers.ModelSerializer):
content_object = NotificationRelatedField(read_only=True)
class Meta:
model = Notification
fields = [
'id',
'created_at',
'is_read',
'type',
'content_object',
]
【问题讨论】:
标签: python django django-rest-framework