我已经创建了自己的自定义 models.TextField 子类,它将类作为其值并将这些值编码为数据库中给定类的 CODE 属性。到目前为止,所有这些都有效,但我想使用 Django-reversion,它会在每次进行更改时序列化模型并将模型保存在版本表中。我将其用于我的应用程序的审核目的。
Reversion 无法序列化我分配给自定义字段的值,声称它们不可序列化。如何为我的对象定义序列化方法?我不能只定义一个 DjangoJSONEncoder 子类(Django 用于默认序列化的类),因为我无法控制调用其序列化方法的代码,因为它是 Django-reversion 的一部分。这是堆栈跟踪,最终在 Python 的默认 json 编码器中失败:
ERROR: Could not save initial version for AccessCircuitRTTicket 3.
Traceback (most recent call last):
File "./manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/reversion/management/commands/createinitialrevisions.py", line 87, in handle
self.create_initial_revisions(app, model_class, comment, batch_size, verbosity)
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/reversion/management/commands/createinitialrevisions.py", line 123, in create_initial_revisions
default_revision_manager.save_revision((obj,), comment=comment)
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/reversion/revisions.py", line 430, in save_revision
for obj in objects
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/reversion/revisions.py", line 430, in <genexpr>
for obj in objects
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/reversion/revisions.py", line 108, in get_version_data
"serialized_data": self.get_serialized_data(obj),
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/reversion/revisions.py", line 91, in get_serialized_data
fields = list(self.get_fields_to_serialize()),
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 122, in serialize
s.serialize(queryset, **options)
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 58, in serialize
self.end_object(obj)
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/django/core/serializers/json.py", line 52, in end_object
cls=DjangoJSONEncoder, **self.json_kwargs)
File "/usr/lib/python2.7/json/__init__.py", line 181, in dump
for chunk in iterable:
File "/usr/lib/python2.7/json/encoder.py", line 427, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "/usr/lib/python2.7/json/encoder.py", line 401, in _iterencode_dict
for chunk in chunks:
File "/usr/lib/python2.7/json/encoder.py", line 401, in _iterencode_dict
for chunk in chunks:
File "/usr/lib/python2.7/json/encoder.py", line 435, in _iterencode
o = _default(o)
File "/home/sgalbraith/.python_virtualenvs/unleash/local/lib/python2.7/site-packages/django/core/serializers/json.py", line 104, in default
return super(DjangoJSONEncoder, self).default(o)
File "/usr/lib/python2.7/json/encoder.py", line 177, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <class 'console.broadband.enumerations.ProvisioningTicket'> is not JSON serializable
在 LazyEncoder 的定义和紧接在它前面的文本中,对这里的答案有一个模糊的建议。