Multiple undos in Django-Reversion

196 views Asked by At

I want to undo my changes done in the model. I can undo it 1-level down using django-reversion. how but do I undo my changes multiple times.

For eg:-

I've a model

with reversion.create_revision():
    server_obj = Server(url = 1)
    server_obj.save()

Now, I update it twice. by the word twice, I mean I called this function twice or say n times.

with reversion.create_revision():
    url = bundle.data['url']
    server_obj.url = url

How do I undo in n-times down.

Currently, I'm doing like this.

your_model = Server.objects.get(id = id)
version_list = reversion.get_unique_for_object(your_model)
version = version_list[1]
version.revision.revert()

How will I do it???

1

There are 1 answers

6
Árni St. Steinunnarson On

version_list.order_by('-revision__date_created')[2].revert()

This however will revert without leaving a revision of it's own.

There is something in the queryset called last() that shows you which version the object held before last change. However it's not usable for your usecase since it doesn't track revert().

The only way I see possible for you is making an external model that tracks undos. That's messy and prone to edge cases. I'm left with the feeling there must be some better way to do this.