I have 2 classes:
class ContactInternal(models.Model):
name = models.CharField(max_length=80)
SysAppApp = apps.get_app_config('SysApp')
SysAppModel = SysAppApp.models
reversion.register(ContactInternal, follow=["fk_contactinternal_sysapp"])
reversion.register(SysAppModel)
# SysApp from another app called 'SysApp'
class SysApp(models.Model):
name = models.CharField(max_length=80)
internalcontact = models.ForeignKey(ContactInternal, related_name='fk_contactinternal_sysapp', null=True, verbose_name="Internal Contact",blank=True,on_delete=models.SET_NULL)
It is working fine for most of the case. e.g. when I make a change to ContactInternal, a new version is made to SysApp. Pretty happy with how it works.
However if I delete a record linked from SysApp.internalcontact
, no version is created for SysApp.
Actually all I want to achieve is to get a timestamp of the last modified date of SysApp and by which user(don't need to rollback). I want to know
'When Peter delete a John from ContactInternal on 26th Feb, it will show Peter updated SysApp on 26th Feb'
How can I do it with django-reversion
? Or is there another way to achieve this?
- Note: There is another field in SysApp which is M2M I would like to achieve the same result too.
Found a solution:
There are 3 steps needed: 1) add 'reversion.middleware.RevisionMiddleware' to MIDDLEWARE_CLASSES in settings.py 2) create a pre_delete signal function to look up the SysApp object 3) explicit call save() for the SysApp obj
Some explaination: 1) By adding the middleware any changes to your models will be added to revision history 2) use a pre_delete signal to find the SysApp object (the parent) 3) call save() to force django-reversion to create a revision
Some references: django-reversion documentation: http://django-reversion.readthedocs.org/en/latest/api.html quick example using signal: http://www.koopman.me/2015/01/django-signals-example/