django软删除不级联删除

发布于 2024-11-18 02:59:37 字数 1197 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半仙 2024-11-25 02:59:37

您的 Person 模型目前不适合“软”删除。当您删除 Person 对象时,Django 将删除所有相关的 Account 对象。如果您想要软删除 Person 对象,请在 Person 模型中为其添加一个标志。然后,您需要确保默认的 Account 管理员排除那些与软删除的 Person 对象相关的帐户。

编辑:

  1. 一种方法是让默认管理器排除与非活动 Person 对象相关的对象,而不是将它们设置为“已删除”:

     类 AccountManager(models.Manager): 
          def get_query_set(self):
              返回 self.filter(person__active=True).filter(active=True)
    
  2. 另一种方法是在 Person 对象出现时将相关的 Account 对象设置为“删除”是软删除的。为此,您可以使用信号。 保存后信号我认为 Person 对象是合适的。

Your Person model is not currently designed for "soft" deletes. When you delete a Person object, Django will remove all related Account objects. If you want to soft-delete Person objects, add a flag for that in the Person model. Then you need to make sure that your default Account manager excludes those accounts that are related to a soft-deleted Person object.

Edit:

  1. One approach is making the default manager exclude the objects related to inactive Person objects instead of setting them "deleted":

      class AccountManager(models.Manager): 
          def get_query_set(self):
              return self.filter(person__active=True).filter(active=True)
    
  2. 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.

忘东忘西忘不掉你 2024-11-25 02:59:37

另一种解决方案是使用 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.

长梦不多时 2024-11-25 02:59:37

不要忘记将其添加到 Meta 类中:

class Meta:
    base_manager_name = "objects"

仅具有:

objects = SoftDeleteManager()

还不够,因为 django 管理确认页面和操作是如何工作的。
指定该选项后,Django 还将在删除实例确认页面中使用“对象”管理器,这将过滤掉软删除的实例。

Don't forget to add this to Meta class:

class Meta:
    base_manager_name = "objects"

having just:

objects = SoftDeleteManager()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文