djangodjango-reversion

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.


Solution

  • 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.