尝试JSON序列化自定义Django模型字段时出现TypeError

时间:2012-06-15 11:05:16

标签: python django json django-serializer django-custom-field

我编写了一个自定义Django模型字段来存储dateutil.relativedelta。该字段如下所示:

class RelativeDeltaField(models.CharField):
    description = "Relative delta"

    __metaclass__ = models.SubfieldBase

    max_length = 200

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = self.max_length
        super(RelativeDeltaField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        if isinstance(value, relativedelta):
            return value

        try:
            return eval(value)
        except SyntaxError, e:
            logging.error("Mangled data for a relativedelta field. Was the data truncated in MySQL column? Either manually fix, or change the next line to 'return None' and manually fix. value=%r message=%r" % (value, e))
            raise

    def get_db_prep_save(self, value, connection):

        return repr(value)

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.get_prep_value(value)

我正在尝试将带有此字段的对象序列化为json:

serializers.serialize('json', obj)

但是我收到了一个错误:

…
  File "/home/rory/tix/virtualenv/lib/python2.6/site-packages/django/core/serializers/json.py", line 58, in default
    return super(DjangoJSONEncoder, self).default(o)
  File "/usr/lib/python2.6/dist-packages/simplejson/encoder.py", line 190, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: relativedelta(hours=+1) is not JSON serializable

使用pdb,我可以看到o在这种情况下是对象本身,它还没有被转换为任何东西,也没有被序列化。

如何解决此问题,以便我可以将我的字段序列化?

Django文档说(https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#converting-field-data-for-serialization)添加value_to_string,我确实有。{{1}}。

1 个答案:

答案 0 :(得分:1)

doc中的代码只是一个样板,您需要实现value_to_string以返回表示relativedelta的字符串。

RelativeDeltaField基于models.CharField,因此get_prep_value()看起来像

def get_prep_value(self, value):
    return self.to_python(value)

CharField没关系,因为该字段的Python中的预期值已经是一个字符串。但是,您的自定义版to_python()会返回relativedelta而不是字符串。