0

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???

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • possible duplicate of [django-reversion undo feature - recovering multiple objects](http://stackoverflow.com/questions/15572746/django-reversion-undo-feature-recovering-multiple-objects) – Árni St. Steinunnarson Nov 13 '14 at 10:32
  • No. I've read & tried that. Doesnt solves my purpose. I'm able to retrieve the list of previously saved objects. But how would undo it multiple times? `version_list` does returns me the list of all previously saved objects. But how do I use it for multiple undo's? – Praful Bagai Nov 13 '14 at 10:35
  • You have to walk up the version list until you find the version you want to restore. – Árni St. Steinunnarson Nov 13 '14 at 11:03
  • Can you please provide a sample code snippet? that will be really helpful. Thanks!!! – Praful Bagai Nov 13 '14 at 11:27

1 Answers1

0

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.

  • Thanks @Beltiras. This will revert directly to the second last update. I want something like this. I posted once. Then updated again (1). and again (2), and again (3), and again (4), and again (5). Now I want to undo step-by-step. First-time Undo should revert to (4), another undo should revert to (3), another undo to (2), another undo to (1), and thats it, no more undo possible after that. – Praful Bagai Nov 14 '14 at 04:17
  • This is clear. It's also hard to do. Try amending one line in your original code: version_list = reversion.get_unique_for_object(your_model).filter(revision_id__lte=your_model.revision_id) – Árni St. Steinunnarson Nov 14 '14 at 09:29
  • what will it do? Please explain. Thank you – Praful Bagai Nov 14 '14 at 09:52
  • Filter away any revisions younger than the one set at the current moment. You should really try out the code provided and play with it in the REPL. It's the only way to understand complex systems: get your hands dirty in them. – Árni St. Steinunnarson Nov 14 '14 at 09:54
  • Well I tried. What is `your_model.re‌​vision_id`?? My model doesnt have any `revision_id` – Praful Bagai Nov 14 '14 at 09:59
  • Sorry, made a mistake, I'll amend the answer with my new finding. – Árni St. Steinunnarson Nov 14 '14 at 10:05