11

I did the initial installation steps and created the initial revisions, but then when I save a model in django shell, the new revision is not created:

In [1]: s = Shop.objects.all()[0]
In [2]: import reversion
In [3]: s.name = 'a'
In [4]: s.save()
In [5]: s.name = 'b'
In [6]: s.save()

In [7]: reversion.get_for_object(s)
Out[7]: [<Version: <1> "X">]

This is the initial revision.

When I update the model from a view, a revision is created successfully.

What am I missing?

The models.py file is:

 ...
 class Shop(Model):
     ...


 import reversion
 reversion.register(Shop)
 <EOF>

I see a reversion method among post_save receiver, although it isn't called when I debug it.

I have Django v1.4.1, reversion v1.6.2.

culebrón
  • 34,265
  • 20
  • 72
  • 110
  • [This thread](https://groups.google.com/forum/?fromgroups=#!msg/django-reversion/s7p3nTemR04/tMTF1ZuOjmEJ) sheds some light on the issue. Where are you registering your models with revisions? – David Robinson Sep 03 '12 at 06:59
  • Could you try putting the lines `import reversion; reversion.register(Shop)` at the bottom of the `models.py` file where you define `Shop`, and see if that fixes the problem? – David Robinson Sep 03 '12 at 07:02
  • @DavidRobinson: It's already there, right at the bottom. And revisions are saved in views only. – culebrón Sep 03 '12 at 10:10

1 Answers1

34

I wrote django-reversion, so I think I can shed some light on this issue.

A Version of a model is automatically saved when a model is saved, providing the following are true:

  1. The model is registered with django-reversion.
  2. The code block is marked up as being within a revision.

Point 1 can be achieved by either registering a model with VersionAdmin, or explicitly calling reversion.register() in your models.py file.

Point 2 can be achieved by using RevisionMiddleware, or the reversion.create_revision() decorator or context manager. Any admin views in VersionAdmin also save a revision.

So, if your shell is not creating Versions, then either point 1 or point 2 is not being met. Here's how to fix it:

  1. If you're using VersionAdmin, import the relevant admin module in your shell code to kick in the auto-registration. Alternatively, call reversion.register() in your models.py file.
  2. In your shell code, using the reversion.create_revision() context manager around your call to save.
with reversion.create_revision():
    s.save()

More about this sort of thing on the Low Level API wiki page:

http://django-reversion.readthedocs.org/en/latest/api.html

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Dave
  • 688
  • 1
  • 7
  • 10
  • This makes sense and I took the liberty to the https://github.com/etianen/django-reversion/wiki/Low-level-API wiki page .... that said what is the best to ensure that a model save() is always creating a version? signals? save() override? – Philippe Ombredanne Dec 07 '12 at 12:37
  • Great tip (#2). I suggest this is added to the installation instructions of django-reversion since I had it disabled and didn't notice this until I found this post... – Davy Dec 28 '16 at 16:24