django软删除不级联删除
我在 django 管理中使用软删除,就像 这个。 问题是,当我删除外键项目时,它似乎不会触发其链接到的所有项目的删除。或者也许它确实如此,但它没有运行我的模型上的自定义def delete。
-如果我删除一个人,那么他们会被软删除,但相关帐户不会受到影响。
-如果我删除软删除,那么当我删除一个人时,帐户也会被删除,这是正确的。
因此,理想情况下,当我删除一个人时,我希望它软删除该人,并且引用该人的帐户也被软删除(将它们标记为不活动)。
class Person(models.Model):
description = models.CharField(max_length=100)
def delete(self, *args, **kwargs):
self.active = False
self.deleted_date = datetime.now()
self.save()
class Account(models.Model):
name = models.CharField(max_length=50)
person = models.ForeignKey(Person, null=True, blank=True)
active = models.BooleanField(default=True, editable=False)
objects = SoftDeleteManager()
def delete(self, *args, **kwargs):
self.active = False
self.deleted_date = datetime.now()
self.save()
def __unicode__(self):
return "%s: %s" % (self.type,self.name)
更新:我已经更新了问题。我没有说过我正在对 Person 模型运行软删除。还补充说,当 def 删除未被覆盖时,级联删除可以工作,但是当我覆盖删除时,级联不会触发。
I'm using a soft delete in my django admin, done like this.
The issue is that when I delete a foreign key item, that it doesn't seem to trigger the deletes for all the items it's linked to. Or maybe it does but it's not running the custom def delete I have on my model.
-If I delete a person then they are soft-deleted, but the related account is left untouched.
-If I remove the soft deletes, then when I delete a Person, the Accounts are deleted too which is correct.
So ideally when I delete a Person I'd want it to soft delete the Person and the Accounts referencing the Person to also be soft deleted(marking them inactive).
class Person(models.Model):
description = models.CharField(max_length=100)
def delete(self, *args, **kwargs):
self.active = False
self.deleted_date = datetime.now()
self.save()
class Account(models.Model):
name = models.CharField(max_length=50)
person = models.ForeignKey(Person, null=True, blank=True)
active = models.BooleanField(default=True, editable=False)
objects = SoftDeleteManager()
def delete(self, *args, **kwargs):
self.active = False
self.deleted_date = datetime.now()
self.save()
def __unicode__(self):
return "%s: %s" % (self.type,self.name)
UPDATE: I have updated the question. I had not said that I'm running a soft delete on the Person model. Also added that when the def deletes are not overridden that the cascading deletes work, but when I do override the delete, the cascading doesn't trigger.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
Person
模型目前不适合“软”删除。当您删除Person
对象时,Django 将删除所有相关的Account
对象。如果您想要软删除Person
对象,请在Person
模型中为其添加一个标志。然后,您需要确保默认的Account
管理员排除那些与软删除的 Person 对象相关的帐户。编辑:
一种方法是让默认管理器排除与非活动 Person 对象相关的对象,而不是将它们设置为“已删除”:
另一种方法是在 Person 对象出现时将相关的 Account 对象设置为“删除”是软删除的。为此,您可以使用信号。 保存后信号我认为 Person 对象是合适的。
Your
Person
model is not currently designed for "soft" deletes. When you delete aPerson
object, Django will remove all relatedAccount
objects. If you want to soft-deletePerson
objects, add a flag for that in thePerson
model. Then you need to make sure that your defaultAccount
manager excludes those accounts that are related to a soft-deleted Person object.Edit:
One approach is making the default manager exclude the objects related to inactive Person objects instead of setting them "deleted":
Another approach would be setting your related Account objects "deleted" when a Person object is soft-deleted. For that, you could use a signal. A post-save signal on Person objects would be appropriate I think.
另一种解决方案是使用 django-softdelete,这是最近出现在 github 上的 Django 包: https:// github.com/scoursen/django-softdelete
如果您为模型使用提供的 SoftDeleteObject mixin,删除它们将自动导致所有相关模型的软删除。更好的是,它存储与软删除相关的所有模型,因此您还可以通过单个取消删除调用来取消删除所有模型。
An alternative solution would be to use django-softdelete, a Django package that has recently appeared on github: https://github.com/scoursen/django-softdelete
If you use the supplied SoftDeleteObject mixin for your models, deleting them will automatically result in a soft deletion also of all the related models. Even better is that it stores all models related in the soft deletion, so that you can also undelete all of them with a single undelete call.
不要忘记将其添加到 Meta 类中:
仅具有:
还不够,因为 django 管理确认页面和操作是如何工作的。
指定该选项后,Django 还将在删除实例确认页面中使用“对象”管理器,这将过滤掉软删除的实例。
Don't forget to add this to Meta class:
having just:
Is not enough, because how django admin confirmation pages and actions work.
Having that option specified, Django will use "objects" manager also for example in delete instance confirmation pages, which will filter out soft deleted instances.