def __init__(self, *args, **kwargs): """Initializes the VersionAdmin""" super(VersionAdmin, self).__init__(*args, **kwargs) # Automatically register models if required. if not revision.is_registered(self.model): inline_fields = [] for inline in self.inlines: inline_model = inline.model self._autoregister(inline_model) if issubclass(inline, GenericInlineModelAdmin): ct_field = inline.ct_field ct_fk_field = inline.ct_fk_field for field in self.model._meta.many_to_many: if ( isinstance(field, GenericRelation) and field.object_id_field_name == ct_fk_field and field.content_type_field_name == ct_field ): inline_fields.append(field.name) elif issubclass(inline, (admin.TabularInline, admin.StackedInline)): fk_name = inline.fk_name if not fk_name: for field in inline_model._meta.fields: if isinstance(field, models.ForeignKey) and issubclass(self.model, field.rel.to): fk_name = field.name accessor = ( inline_model._meta.get_field(fk_name).rel.related_name or inline_model.__name__.lower() + "_set" ) inline_fields.append(accessor) self._autoregister(self.model, inline_fields)
def _autoregister(self, model, follow=None): """Registers a model with reversion, if required.""" if not revision.is_registered(model): follow = follow or [] for parent_cls, field in model._meta.parents.items(): if field: # Proxy models do not have a parent field. follow.append(field.name) self._autoregister(parent_cls) revision.register(model, follow=follow, format=self.reversion_format)
import datetime from django.db import models from django.contrib.auth.models import User from reversion_relations.fields import ReversionForeignKey from reversion.revisions import revision class Supplier(models.Model): name = models.CharField(max_length=100) location = models.TextField(blank=True) salary = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True) starred = models.BooleanField(default=False, blank=True) points = models.IntegerField(default=0, blank=True) date_foundation = models.DateField(blank=True, null=True) def __unicode__(self): return self.name class Purchase(models.Model): date = models.DateTimeField(blank=True, default=datetime.datetime.now) supplier = ReversionForeignKey(Supplier, null=True, blank=True) user = models.ForeignKey(User, null=True, blank=True) if not revision.is_registered(Supplier): revision.register(Supplier) if not revision.is_registered(Purchase): revision.register(Purchase)