【发布时间】:2015-12-15 11:23:12
【问题描述】:
假设我在 django 中有以下模型结构:
class A(models.Model):
x = models.IntegerField()
def copy(self):
obj = self
obj.pk = None
obj.save()
return obj
class B(A):
y = models.IntegerField()
def copy(self):
# this method is what I am confused about
new_parent = super(B, self).copy() # not sure about this
obj = self
obj.pk = None
# how to set obj's parent model to 'new_parent'
obj.save()
return obj
我不确定如何访问父模型的对象,以及如何使此复制方法起作用?
我已经搜索了很多,但找不到任何答案。我应该只使用一对一的关系吗?
【问题讨论】:
-
我认为您需要对父模型对象使用外键关系
标签: python django inheritance