Dmitrii Mikhailov
Dmitrii Mikhailov

Reputation: 5231

New revision isn't created when saving model

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.

Upvotes: 4

Views: 927

Answers (2)

DrRobotNinja
DrRobotNinja

Reputation: 1421

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.

Upvotes: 4

yilmazhuseyin
yilmazhuseyin

Reputation: 6612

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

Upvotes: 0

Related Questions