如何删除管理中的对象并保留父级?

发布于 2024-11-15 03:01:01 字数 519 浏览 4 评论 0原文

我有以下问题:

class Gift(models.Model):                                            
    name        = models.CharField(max_length=255,default='')

class ProblematicGift(Gift):
    # it does not help  gift_ptr = models.OneToOneField(Gift, parent_link=True, default=None, null=True, blank=True, on_delete=models.DO_NOTHING)
    notes       = models.CharField(max_length=255,default='')

如何在管理界面中删除 ProblematicGift 的对象并保留 Gift 的对象?

简化的背景:自动选择有问题的礼物并将其添加到表中,管理员在其中查看它,修复礼物并删除有问题的礼物

I have following problem:

class Gift(models.Model):                                            
    name        = models.CharField(max_length=255,default='')

class ProblematicGift(Gift):
    # it does not help  gift_ptr = models.OneToOneField(Gift, parent_link=True, default=None, null=True, blank=True, on_delete=models.DO_NOTHING)
    notes       = models.CharField(max_length=255,default='')

How I can delete the object of ProblematicGift in admin interface and keep the object of Gift ?

Simplified background: Automat select problematic gift and add it to table, where admin look at it, fix the gift and delete the ProblematicGift

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

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

发布评论

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

评论(2

悲凉≈ 2024-11-22 03:01:01

您有三种选择:

  1. 最快和最简单的方法是基于 ProblematicGift 创建一个新的 Gift,然后删除 ProblematicGift

  2. 您可以使用抽象继承将 Gift 设为原始类型,然后将其子类化以创建 ProblematicGift 以及诸如 GoodGifts 之类的内容。之后的过程几乎相同:它们各自获得单独的表,因此您添加一个 GoodGift,然后删除 ProblematicGift。它与 #1 几乎相同,但语义更多一些。

  3. 可能是您最好的选择:使用代理模型。您可以将布尔属性添加到“is_problematic”等形式的礼物中。然后,创建 ProblematicGift 作为 Gift 的代理,在创建时自动将 is_problematic 设置为 True,并覆盖管理器以仅返回带有 的礼物>is_problematic 设置为 True。然后,您只需将该属性设置为 False,而不是删除 ProblematicGift,它就会保留查询集。

--

class Gift(models.Model):
    name           = models.CharField(max_length=255,default='')
    notes          = models.CharField(max_length=255,default='')
    is_problematic = models.BooleanField(default=False)

class ProblematicGiftManager(models.Manager):
    def get_query_set(self, *args, **kwargs):
        qs = super(ProblematicGiftManager, self).get_query_set(*args, **kwargs)
        return qs.filter(is_problematic=True)

class ProblematicGift(models.Model):

    objects        = ProblematicGiftManager()

    class Meta:
        proxy = True

    def save(self, *args, **kwargs):
        # Make sure it's new
        if not self.pk: 
            self.is_problematic = True

        super(ProblematicGift, self).save(*args, **kwargs)

    def resolve(self):
        self.is_problematic = False
        self.save()

编辑:注释ProblematicGift移至Gift。使用代理模型时,您无法向子类添加任何新字段。

You have three choices:

  1. Quickest and hackiest is to just create a new Gift based on ProblematicGift and then delete ProblematicGift.

  2. You can use abstract inheritance to make Gift a primitive type and then subclass it to create ProblematicGifts and something like GoodGifts. The procedure after that is pretty much the same: they each get separate tables, so you add a GoodGift and then delete the ProblematicGift. It's pretty much the same as #1, but a little more semantic.

  3. Is probably your best choice: using proxy models. You add an boolean attribute to gift of the form of something like 'is_problematic'. Then, create ProblematicGift as a proxy for Gift that automatically sets is_problematic to True on creation, and override the manager to only return gifts with is_problematic set to True. Then, you simply set that attribute to False instead of deleting ProblematicGift and it leaves the queryset.

--

class Gift(models.Model):
    name           = models.CharField(max_length=255,default='')
    notes          = models.CharField(max_length=255,default='')
    is_problematic = models.BooleanField(default=False)

class ProblematicGiftManager(models.Manager):
    def get_query_set(self, *args, **kwargs):
        qs = super(ProblematicGiftManager, self).get_query_set(*args, **kwargs)
        return qs.filter(is_problematic=True)

class ProblematicGift(models.Model):

    objects        = ProblematicGiftManager()

    class Meta:
        proxy = True

    def save(self, *args, **kwargs):
        # Make sure it's new
        if not self.pk: 
            self.is_problematic = True

        super(ProblematicGift, self).save(*args, **kwargs)

    def resolve(self):
        self.is_problematic = False
        self.save()

EDIT: Moved note from ProblematicGift to Gift. When using proxy models, you can't add any new fields to the subclass.

谁与争疯 2024-11-22 03:01:01

老实说,您犯的错误是试图从 Gift 继承。您不想为您的用例这样做。

最好的方法是使 Gift 成为独立模型:

class Gift(models.Model):                                            
    name = models.CharField(max_length=255,default='')

然后让 ProblematicGift 引用它:

class ProblematicGift(models.Model):
    gift = models.OneToOneField(Gift, null=True, blank=True)
    notes = models.CharField(max_length=255,default='')

    # this method added based on a comment
    def __unicode__(self):
        return self.gift.name

现在您可以安全地删除 ProblematicGift。

Honestly, the mistake you're making is trying to inherit from Gift. You don't want to do that for your use case.

The best way is to make Gift a stand-alone model:

class Gift(models.Model):                                            
    name = models.CharField(max_length=255,default='')

And then have ProblematicGift reference it:

class ProblematicGift(models.Model):
    gift = models.OneToOneField(Gift, null=True, blank=True)
    notes = models.CharField(max_length=255,default='')

    # this method added based on a comment
    def __unicode__(self):
        return self.gift.name

Now you can delete the ProblematicGift safely.

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