pythondjangodjango-reversion

Django-reversion : incompatible version data during field_dict access


I have the following script:

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__)

Here's the very long traceback:

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.

So obviously something is wrong with field_dict, during deserialization.

I then tried to restart all revisions and do it again:

python manage.py deleterevisions

I ran it again, and it still gave the same error.

My schema is fairly simple:

@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)

What exactly is going on?

Note: If I remove that specific line, here's my output(after running python manage.py deleterevisions to refresh):

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

Solution

  • So I figured out the reason. For some reason, passing in a datetime object into the DateField type of my models is not valid. I was not aware there was a DateTimeField which is exactly for this purpose.

    Django did not complain when I passed in a datetime object and it appears that it probably does implicit casting. However, the library django-reversion probably took the value before implicit conversion and tried to deserialize the object into the model which did not prove to be successful with a datetime object.

    As per documentation for DateTimeField:

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