Задать вопрос

Тел: +7 965 3737 888





Full Model History

<p>This was a wild experiment, which appears to work!
<p>One model holds all the data, from every object version ever to exist. The other model simply points to the latest object from its gigantic brother. All fields can be accessed transparently from the little model version, so the user need not know what is going on. Coincidently, Django model inheritance does exactly the same thing, so to keep things insanely simple, that's what we'll use:
class EmployeeHistory(FullHistory):
    name = models.CharField(max_length=100)

class Employee(EmployeeHistory):
<p>That's it! Django admin can be used to administer the Employee and every version will be kept as its own EmployeeHistory object, these can of course all be browsed using the admin :-)
<p>This is early days and just a proof of concept. I'd like to see how far I can go with this, handling ForeignKeys, ManyToManyFields, using custom managers and so on. It should all be straightforward, especially as the primary keys should be pretty static in the history objects...
<p><em>updated 3 August 2009 for Django 1.1 and working date_updated fields</em>

Вопрос полезен? Да0/Нет0

Ответы (5):

Ответ полезен? Да0/Нет0

is this currently working for you against django 1.1 rc?

when i try to use it, i get the following error on save of an "Employee" instance:

/Users/paul/Documents/web_development/django_sites/test_project/history/models.pyc in save(self, *args, **kwargs)
     19             # Duplicate and reassign parent
---> 20             for model, field in self._meta.parents:
     21                 if getattr(self, '%s_id' % field.name) is not None:
     22                     rel_obj = getattr(self, field.name)

TypeError: 'ModelBase' object is not iterable

Ответ полезен? Да0/Нет0

Take a look at django-reversion. That has admin integration and will work on third party apps just by changing a line in their admin.py

Ответ полезен? Да0/Нет0

I am also highly interested in this, as we've done something similar at work. Originally we intended to do the history at the DB level, but after designing the models to support that (a custom model that did nothing but provide read access to history entries) we decided to use Django for it.

Looking forward to seeing what you come up with as you expand it!

Ответ полезен? Да0/Нет0

Thanks! I'll look at expanding on this and making it comprehensive. I've just realised what a cool snippet number I have. Excellent.

Ответ полезен? Да0/Нет0

Brilliant. Way to get around all the hard stuff in the AuditTrail wiki page in one fell swoop.