Django admin:删除链接元素

发布于 2024-09-13 14:41:57 字数 115 浏览 1 评论 0原文

在 Django 管理站点中,当您决定抑制某个对象时,所有链接的元素(即:外键指向的元素)也会被删除。

除了在 shell 中进行原始查询之外,如何避免这种情况? 是否可以调整管理员以进行选择? 谢谢

in Django admin site when you decide to suppress an object, all the linked element (ie: elements pointed at by a foreign key) are also deleted.

How do you dodge this, except making raw queries in the shell?
Is it possible to tune the admin to have the choice?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

心房敞 2024-09-20 14:41:57

您可以绕过它,但没有正确的方法来做到这一点。

查看这些链接以了解可能的解决方法:

You can hack around it, but there is no proper way of doing it.

Take a look at these links for possible workarounds:

子栖 2024-09-20 14:41:57

您只需要覆盖相关模型的删除方法即可。

通用示例:

class Foo:

    def delete(self):
        """
        Override default model method so that all objects in the related
        objects set are not removed
        """
        self.my_related_stuff.clear()
        super(Foo, self).delete()

有关更多示例,请参阅本文:

http://fragmentsofcode.wordpress.com/2009/03/06/django-gotcha-lated-objects-deleted-by-default/

You just need to override the delete method of the models in question.

Generic example:

class Foo:

    def delete(self):
        """
        Override default model method so that all objects in the related
        objects set are not removed
        """
        self.my_related_stuff.clear()
        super(Foo, self).delete()

See this article for more examples:

http://fragmentsofcode.wordpress.com/2009/03/06/django-gotcha-related-objects-deleted-by-default/

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