克隆继承的django模型实例

时间:2013-04-09 16:11:26

标签: python django django-models

当我克隆django模型实例时,我习惯清理'pk'字段。 这似乎不适用于继承的模型:

拿这个:

class ModelA(models.Model):
    info1 = models.CharField(max_length=64)

class ModelB(ModelA):
    info2 = models.CharField(max_length=64)

class ModelC(ModelB):
    info3 = models.CharField(max_length=64)

现在让我们创建一个实例并按照“通常”的方式克隆它(我使用的是django shell):

In [1]: c=ModelC(info1="aaa",info2="bbb",info3="ccc")

In [2]: c.save()

In [3]: c.pk
Out[3]: 1L

In [4]: c.pk=None  <------ to clone

In [5]: c.save()   <------ should generate a new instance with a new index key

In [6]: c.pk       
Out[6]: 1L         <------ but don't

In [7]: ModelC.objects.all()
Out[7]: [<ModelC: ModelC object>]   (only one instance !)

我找到的唯一方法就是:

In [16]: c.pk =None

In [17]: c.id=None

In [21]: c.modela_ptr_id=None

In [22]: c.modelb_ptr_id=None

In [23]: c.save()

In [24]: c.pk
Out[24]: 2L    <---- successful clone containing info1,info2,info3 from original instance

In [25]: ModelC.objects.all()
Out[25]: [<ModelC: ModelC object>, <ModelC: ModelC object>]

我觉得非常难看,有没有更好的方法从继承的模型中克隆一个实例?

1 个答案:

答案 0 :(得分:0)

c=ModelC(info1="aaa",info2="bbb",info3="ccc")
# creates an instance

c.save()
# writes instance to db

c.pk=None
# I doubt u can nullify the auto-generated pk of an existing object, because a pk is not nullable
c.save()
# if I'm right nothing will happen here.

所以c将永远是同一个对象。如果要克隆它,则需要生成一个新对象。使用ModelC中的构造函数:

def __init__(another_modelC_obj=null, self):
   if another_modelC_obj:
      # for every field in another_modelC_obj: do self.field = another_modelC_obj.field
   super().__init__()

所以你可以去

c2=ModelC(c)

或直接用以下方式调用:

c2=ModelC(c.info1, c.info2, c.info3)

然后,尽管pk

,c2和c仍然是相同的