Django - 删除对象,保留父对象?

发布于 2024-09-18 23:36:49 字数 1169 浏览 2 评论 0原文

我遇到以下多表继承情况:

from django.db import Models

class Partner(models.Model):
    # this model contains common data for companies and persons
    code = models.CharField()
    name = models.CharField()

class Person(Partner):
    # some person-specific data
    ssn = models.CharField()

class Company(Partner):
    # some company-specific data
    tax_no = models.CharField()

如何将 Company 实例转换为 Person 实例,反之亦然?
假设有人错误地使用人员详细信息创建了一个 Company 实例:

company = Company(name="John Smith", tax_no="<some-ssn-#>")

我想将所有错误的 Company 对象(本来应该是 Persons)转换为 Person 对象,并保留所有相关记录(我的模型具有 Partner 模型的 FK,因此保持相同的 partner_ptr 值非常重要)。我可以做这样的事情:

person = Person(name=company.name, ssn=company.tax_no, partner_ptr=company.partner_ptr)

到目前为止一切顺利,但是是否可以删除不再需要的 Company 对象?删除 Company 对象也会删除其父 Partner 对象(以及与合作伙伴相关的任何对象,包括新创建的 Person 对象)。

有什么建议吗?谢谢!

PS:这是一个已经部署的系统,其中包含大量数据,不可能重新设计整个合作伙伴-个人-公司继承概念。

I have the following multi-table inheritance situation:

from django.db import Models

class Partner(models.Model):
    # this model contains common data for companies and persons
    code = models.CharField()
    name = models.CharField()

class Person(Partner):
    # some person-specific data
    ssn = models.CharField()

class Company(Partner):
    # some company-specific data
    tax_no = models.CharField()

How can I convert a Company instance to a Person one, and vice-versa?
Let's say someone mistakenly created a Company instance with person's details:

company = Company(name="John Smith", tax_no="<some-ssn-#>")

I want to convert all wrong Company objects (that were meant to be Persons) to Person objects, keeping all related records (I have models with FKs to the Partner model, so it's important to keep the same partner_ptr value). I can do something like this:

person = Person(name=company.name, ssn=company.tax_no, partner_ptr=company.partner_ptr)

So far so good, but is it possible to delete the Company objects that are not needed anymore? Deleting the Company object will also delete it's parent Partner object (and any related to the partner, including the newly created Person object).

Any recommendations? Thanks!

P.S.: This is an already deployed system, with lots of data in it and it is not possible to redesign the whole Partner-Person-Company inheritance concept.

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

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

发布评论

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

评论(1

浪漫人生路 2024-09-25 23:36:49

解决此问题的一种方法是首先为每个等待删除的公司添加一个虚拟合作伙伴。之后,您可以将所有不需要的 Company 实例的 partner_ptr 更新为适当的虚拟合作伙伴实例。最后您可以删除所有公司。

当然,您可以使用 South 来帮助完成此操作。

更新

进行了一些基本测试,这有效。我正在使用 Django 1.2.1。

我已经尝试过这个,这是不可能的:在 1 中:Company.objects.get(pk=7924 ) Out1: 在 [2] 中:c.partner_ptr = Partner() 在 [3] 中:c.pk在[4]中:c.delete() AssertionError: Company 对象无法删除,因为它的partner_ptr_id 属性设置为None。将partner_ptr实例设置为虚拟实例会改变公司的PK,而不是公司。

您必须附加一个新的合作伙伴,然后保存该公司。然后您就可以安全地删除它了。

所以:

company = Company.objects.get(pk=7924)
dummy_partner = Partner(code = "dummy", name = "dummy")
company.partner_ptr = dummy_partner
company.save()
company.delete()

One way to go about this would be to first add a dummy Partner per each company awaiting deletion. After that you can update the partner_ptr of all unwanted Company instances to the appropriate dummy partner instance. Finally you can delete all the companies.

Of course you can use South to help do this.

Update

Did some rudimentary testing and this works. I am using Django 1.2.1.

I have tried this, it's not possible: In 1: Company.objects.get(pk=7924) Out1: In [2]: c.partner_ptr = Partner() In [3]: c.pk In [4]: c.delete() AssertionError: Company object can't be deleted because its partner_ptr_id attribute is set to None. Setting the partner_ptr instance to a dummy one changes the company's PK and it's not the Company.

You have to attach a new Partner and then save the company. Then you can safely delete it.

So:

company = Company.objects.get(pk=7924)
dummy_partner = Partner(code = "dummy", name = "dummy")
company.partner_ptr = dummy_partner
company.save()
company.delete()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文