Quantcast
Channel: bgola » coding
Viewing all articles
Browse latest Browse all 10

Django model history with django-reversion

$
0
0

I’m currently working on a django project and I needed to track all changes to a model and provide a log showing what has changed (like a wiki).

I’ve found three extensions/libs that apparently could do the job for me (fullhistory, django-history and django-reversion) so I decided to test them, but because of the lack of structure/documentation of the other two I tried just the last one.

django-reversion

(http://code.google.com/p/django-reversion/)

It has a good documentation, integration with the admin and the most important for me a low level api, as I am not using the admin app for this project.

The installation went pretty well (svn co http://django-reversion.googlecode.com/svn/tags/1.1.2/src/reversion to your PYTHONPATH, add the reversion app to your INSTALLED_APPS in settings.py and the usual python manage.py syncdb).

To track the changes of a model you must register it with the reversion framework (importing reversion and calling reversion.register(YourModel)).

Reversion provides some ways for creating revisions of your model and the docs recommend that you choose one of them to use in your project. I chose the MiddleWare method because it seemed the better option for our project, but you should look the reversion documentation and decide which one is the best for you.

After registering the model and installing the MiddleWare no more changes are needed, whenever I save a model a new revision is created on the database. Another cool feature is the follow argument when registering a model. That way you can specify a ForeingKey/ManyToMany field to follow. This means that when you save the model a new revision is created with all “followed” fields in its current state. It’s very useful. See the docs for an example.

The only thing that’s not so clear for me is: what happens if I delete a field from a model that’s registered within reversion? Can you still restore its versions?


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images