将一个模型A复制到模型B中出现错误

时间:2018-09-14 17:43:52

标签: python django-models model copy

我试图将模型A复制到模型B,但是由于某种原因,我遇到了唯一约束失败:appname_duplicate.id

Class orignal(models.Model):
  managed = True
  [some_fields]
  def __unicode__(self):
     return self.recipient_email

Class duplicate(models.Model):
  managed = True
  [same_as_Orignal_fields]
  def __unicode__(self):
     return self.recipient_email

功能:

def clone_orignal(source, destination):
    duplicate = list()
    for row in source.objects.all().values():
      if row not in duplicate:
        duplicate.append(destination(**row))
    if duplicate:
      destination.objects.bulk_create(duplicate)

有人可以看看吗?

0 个答案:

没有答案