Search code examples
pythondjangoundodjango-reversion

django-reversion undo feature - recovering multiple objects


I'm trying to implement 'undo' feature in django project with django-reversion in a case when user can accidentally modify multiple objects. Using admin panel won't work as one has to revert objects one-by-one.

My problem is that I am unable to create revision that holds data about more than one object.
Even when I do

with reversion.create_revision():
    Res.object.all().delete()

then I cannot access revision that groups this change. It's splitted in a "one object one revision.models.Version" manner.

In [103]: reversion.models.Version.objects.all()
Out[103]: [<Version: #00001>, <Version: #00002>]

I've tried also through

reversion.models.Revision.objects.all().order_by('-date_created')[0].version_set.all()

but it also returned only one Version for one deleted Res object. Seems like I'm missing something.


Solution

  • I finally got it.

    Issue 1: Store changes related to multiple objects in a single Revision

    Solution: add TransactionMiddleware to MIDDLEWARE_CLASSES

    Issue 2: Undo feature / reverting changes

    Well, this is confusing. You cannot REVERT CHANGES you can RESTORE OBJECT to previously known state. And using reversion.create_revision() in your object-altering views stores data about that object after changing it. If you have view that modify multiple object then you have to create revision before making actual changes.

    This is also visible in admin panel. If you change your object than a new log entry will appear. But if you want to revert that change than you cannot select newest version -- it is identical with the current state of the object -- instead you have to choose the previous one.