Django-reversion:field_dict访问期间不兼容的版本数据

时间:2018-04-24 21:42:50

标签: python django django-reversion

我有以下脚本:

import django
import os
import sys
from django.db import transaction
from xxx.models import File
import reversion
from reversion.models import Version
from datetime import datetime

# Creating a file entry in here:
with reversion.create_revision():
    f = File(name="foo", mtime=datetime.now(), ctime=datetime.now(), size=33)
    f.save()

    reversion.set_comment("Revision 1")

# Modifying it:
with reversion.create_revision():
    f.mtime = datetime.now()
    f.ctime = datetime.now()
    f.size = 23
    f.save()

    reversion.set_comment("Revision 2")

versions = Version.objects.get_for_object(f)
print(len(versions))
for i in range(len(versions)):
    print(versions[i])
    print(versions[i].field_dict) # ERROR!
    print(versions[i].revision.__dict__)

这是非常长追溯:

Traceback (most recent call last):
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/django/core/serializers/python.py", line 166, in Deserializer
    data[field.name] = field.to_python(field_value)
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1246, in to_python
    params={'value': value},
django.core.exceptions.ValidationError: ["'2018-04-24T21:38:14.319' value has an invalid date format. It must be in YYYY-MM-DD format."]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/reversion/models.py", line 241, in _object_version
    return list(serializers.deserialize(self.format, data, ignorenonexistent=True))[0]
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/django/core/serializers/json.py", line 69, in Deserializer
    yield from PythonDeserializer(objects, **options)
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/django/core/serializers/python.py", line 168, in Deserializer
    raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)
django.core.serializers.base.DeserializationError: ["'2018-04-24T21:38:14.319' value has an invalid date format. It must be in YYYY-MM-DD format."]: (xxx.file:pk=foo) field_value was '2018-04-24T21:38:14.319'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "play_with_reversion.py", line 47, in <module>
    print(versions[i].field_dict)
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/reversion/models.py", line 283, in field_dict
    field_dict = self._local_field_dict
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/reversion/models.py", line 261, in _local_field_dict
    object_version = self._object_version
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/rzhang2/pythons/anaconda2/envs/python36/lib/python3.6/site-packages/reversion/models.py", line 244, in _object_version
    "object_repr": self.object_repr,
reversion.errors.RevertError: Could not load name: foo mtime: 2018-04-24 21:30:15.468835 ctime: 2018-04-24 21:30:15.468838 size: 23 version - incompatible version data.

在反序列化过程中,显然field_dict出现了问题。

然后我尝试重新启动所有修订并再次执行:

python manage.py deleterevisions

我再次运行它,它仍然给出了同样的错误。

我的架构非常简单:

@reversion.register
class File(models.Model):
    name = models.CharField(max_length=400, primary_key=True)
    mtime = models.DateField()
    ctime = models.DateField()
    size = models.IntegerField()

    def __str__(self):
        return "name: {} mtime: {} ctime: {} size: {}".format(self.name, self.mtime, self.ctime, self.size)

到底发生了什么?

注意:如果删除该特定行,这是我的输出(在运行python manage.py deleterevisions后刷新):

3
name: foo mtime: 2018-04-24 21:45:26.497498 ctime: 2018-04-24 21:45:26.497501 size: 23
{'_state': <django.db.models.base.ModelState object at 0x7f6e7aa16f28>, 'id': 42, 'date_created': datetime.datetime(2018, 4, 24, 21, 45, 26, 497470, tzinfo=<UTC>), 'user_id': None, 'comment': 'Revision 2'}
name: foo mtime: 2018-04-24 21:45:26.475707 ctime: 2018-04-24 21:45:26.475711 size: 33
{'_state': <django.db.models.base.ModelState object at 0x7f6e7aa24080>, 'id': 41, 'date_created': datetime.datetime(2018, 4, 24, 21, 45, 26, 470003, tzinfo=<UTC>), 'user_id': None, 'comment': 'Revision 1'}
name: foo mtime: 2018-04-24 ctime: 2018-04-24 size: 23
{'_state': <django.db.models.base.ModelState object at 0x7f6e7aa24198>, 'id': 40, 'date_created': datetime.datetime(2018, 4, 24, 21, 45, 13, 671939, tzinfo=<UTC>), 'user_id': None, 'comment': 'Initial version.'}

1 个答案:

答案 0 :(得分:1)

所以我想出了原因。出于某种原因,将datetime对象传入我的模型的DateField类型无效。我不知道有一个DateTimeField正是出于这个目的。

当我传入一个datetime对象时,Django没有抱怨,而且可能是隐式转换。但是,库django-reversion可能在隐式转换之前获取了值,并尝试将对象反序列化到模型中,该模型未使用datetime对象证明是成功的。

根据DateTimeField的{​​{3}}:

  

日期和时间,由datetime.datetime实例以Python表示。

相关问题