gpt4 book ai didi

python - Django 版本 : incompatible version data during field_dict access

转载 作者:行者123 更新时间:2023-12-01 09:30:26 26 4
gpt4 key购买 nike

我有以下脚本:

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.'}

最佳答案

所以我明白了原因。由于某种原因,将 datetime 对象传递到我的模型的 DateField 类型中是无效的。我不知道有一个 DateTimeField 正是用于此目的。

当我传入 datetime 对象时,Django 没有提示,并且它似乎可能进行了隐式转换。但是,django-reversion 库可能在隐式转换之前获取了该值,并尝试将对象反序列化到模型中,但事实证明,对于 datetime 对象来说,该模型并不成功。

根据documentation对于日期时间字段:

A date and time, represented in Python by a datetime.datetime instance.

关于python - Django 版本 : incompatible version data during field_dict access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50011106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com