3

I use Reversion to control changes in model objects. In documentation I've found this:

Whenever you call save() on a model within the scope of a revision, it will be added to that revision

Here's my code where I use model's save method:

c.save(update_fields=['status'])

When this code is executed there is no new record in revisions list of the object, at least I don't see it in the admin.

Dmitrii Mikhailov
  • 5,053
  • 7
  • 43
  • 69
  • Same for me. The update is recorded in the database but not added into the "revision" table. – jcs Oct 10 '13 at 15:34

2 Answers2

4

I had a similar problem where edits made in the admin interface had reversions, but those in the shell did not.

@yilmazhuseyin is correct, you need the context wrapper, but I found I had an additional bug that my models weren't getting registered.

In admin.py:

class YourModelAdmin(reversion.VersionAdmin):
    pass

admin.site.register(YourModel, YourModelAdmin)

will register your model, but only if the admin code is called. It wasn't being called when I invoked the shell via python manage.py shell

So, to fix this I added to models.py

import reversion
reversion.register(YourModel)

And then when I saved an object, I still needed to use the context wrapper

 with reversion.create_revision():
     obj.save()

Update:

Revision has a few tips for this situation. (http://django-reversion.readthedocs.org/en/latest/api.html#api) One is to simply import your admin module so that the revision gets called.

DrRobotNinja
  • 1,381
  • 12
  • 14
  • You should not need to register the model a second time if you have already registered it in the admin. http://django-reversion.readthedocs.org/en/latest/api.html#api – chrisst Nov 10 '14 at 17:39
  • Agreed. The problem arises if the admin code is not imported. Note the first warning on the page you've referenced. I will update my answer to reflect that. – DrRobotNinja Nov 14 '14 at 17:35
0

I think you need to save model in revision transaction.

Note: If you call save() outside of the scope of a revision, a revision is NOT created. This means that you are in control of when to create revisions.

Source: http://django-reversion.readthedocs.org/en/latest/api.html#creating-revisions

yilmazhuseyin
  • 6,442
  • 4
  • 34
  • 38