简体   繁体   English

删除外键模型对象(django-reversion)后,模型不会创建版本

[英]Model does not create a verion after deleting a foreign key model object (django-reversion)

I have 2 classes: 我有2节课:

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. 在大多数情况下,它工作正常。 eg when I make a change to ContactInternal, a new version is made to SysApp. 例如,当我对ContactInternal进行更改时,将为SysApp创建一个新版本。 Pretty happy with how it works. 对其工作方式非常满意。

However if I delete a record linked from SysApp.internalcontact , no version is created for SysApp. 但是,如果删除从SysApp.internalcontact链接的记录,则不会为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). 实际上,我想要实现的只是获取SysApp上次修改日期的时间戳,以及该用户的时间戳(不需要回滚)。 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 ? 如何使用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. 注意:SysApp中还有一个M2M字段,我也想达到相同的结果。

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 需要3个步骤:1)在settings.py中的MIDDLEWARE_CLASSES中添加'reversion.middleware.RevisionMiddleware'2)创建pre_delete信号函数以查找SysApp对象3)显式调用save()用于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 一些说明:1)通过添加中间件,对模型的任何更改都将添加到修订历史记录中2)使用pre_delete信号查找SysApp对象(父级)3)调用save()强制django-reversion创建修订

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/ 一些参考:django-reversion文档: http ://django-reversion.readthedocs.org/en/latest/api.html使用信号的快速示例: http : //www.koopman.me/2015/01/django-signals-例/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM