Django - 删除对象,保留父对象?
我遇到以下多表继承情况:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此问题的一种方法是首先为每个等待删除的公司添加一个虚拟
合作伙伴
。之后,您可以将所有不需要的Company
实例的partner_ptr
更新为适当的虚拟合作伙伴实例。最后您可以删除所有公司。当然,您可以使用 South 来帮助完成此操作。
更新
进行了一些基本测试,这有效。我正在使用 Django 1.2.1。
您必须附加一个新的
合作伙伴
,然后保存该公司。然后您就可以安全地删除它了。所以:
One way to go about this would be to first add a dummy
Partner
per each company awaiting deletion. After that you can update thepartner_ptr
of all unwantedCompany
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.
You have to attach a new
Partner
and then save the company. Then you can safely delete it.So: