我有以下SerializerField:
class TimestampField(Field):
def to_representation(self, value):
if not value:
return ''
return value.timestamp()
我在序列化程序中这样使用它:
^{pr2}$现在getterdelivery_date_to
可以返回None,我想使用to_representation
方法将其转换为空字符串。但是,当我使用序列化程序解析这个None值时,它甚至不输入to_representation
方法,并立即返回None
。我应该做些什么来对None
使用to_representation
方法?在
默认情况下,序列化程序的
to_representation
方法跳过没有值的字段(请参见source)。在您可以编写mixin类来覆盖默认值
to_representation
:并在序列化程序中使用它:
^{pr2}$相关问题 更多 >
编程相关推荐