views:

130

answers:

3

I'd like to create a feature for my Django app similar to Django admin's 'Recent Actions', in order to store history information on my other models.

For example say I have two models called Book and Author. I want to have a third model that stores information such as what action was performed on a given object in a model (add, modify, delete, etc.) by who and when.

Who, when and the action are easy, I'm just unsure about how to store information regarding what object the action was performed on.

My initial idea was to have a 'Transactions' model that would store this information, and both my Book and Author models could have a ForeignKey relation to it. However, if I delete the given book or author, then its transaction history is also deleted and I have no record that this object was indeed deleted.

I've been thinking of other possible solutions, but I thought I'd ask for more experienced opinions here first. How should I approach this problem and what are some reasonable solutions to it?

Thanks!

+2  A: 

You might want to check out django-reversion -- Either to implement or to learn from it for your own implementation. Django reversion works by saving information about the change, along with a serialized copy of the item as it existed at that one moment. This means that you can later recall that one specific item and do a comparison or restoration or whatever.

If you decide to implement your own custom solution, I'd recommend making use of Django's built in signal sub-system. Django out-of-the-box provides the common signals for things you mentioned like model creation, saving, and deleting. You could add in custom signals as well if you found the need for them.

Using signals would allow you to develop the 'history' code separate from the Book/Author implementation code which in the long run will be an asset (as opposed to stringing history code throughout the Book/Author code). Again django-reversion serves as a good reference here by doing its work almost entirely with signals.

T. Stone
Thanks, that's really helpful. django-revision seems to be too much for my needs, but the signal system is really gonna help me out here.
hora
A: 

You also can try fullhistory that manage changes outside of the admin inteface. I had some issues when testing it, but it may work for you.

ArBaDaCarBa
+1  A: 

IMHO the best solution is the one developed by Marty Alchin in his Pro Django book which unfortunately costs money but, fortunately, is a book worth getting anyway.

An early version of his audit trail can be found on the Django wiki at AuditTrail but I'm not sure how well this code would work in recent versions of Django.

Van Gale