将模型对象ID从模型复制到Django中的另一个模型

时间:2018-12-20 13:34:49

标签: python django

我想将数据从一个模型复制到另一个模型,我找到了一个示例Copy Model Object From a Model To Another In Django,但出现错误:

  

AttributeError:“ BigAutoField”对象没有属性“ wid”。

我希望tobjects模型中的widId模型中获得w_orders。这是我的代码:

models.py

class w_orders(models.Model): 
    Id = models.BigAutoField(primary_key=True)
    datedefwo = models.DateField(default=datetime.now)
    datesched = models.DateField(blank=True, null=True)
    datefinished = models.DateField(blank=True, null=True)
    sign = models.BigIntegerField(blank=True, null=True)
    statusid = models.BigIntegerField(blank=True, null=True, default=1, choices=STATUS_CHOICES)
    typeid = models.BigIntegerField(blank=True, null=True, default=1, choices=TYPE_CHOICES)
    comments = models.CharField(max_length=254, blank=True, null=True)
    navid = models.BigIntegerField(blank=True, null=True)
    navkonsid = models.CharField(max_length=12, blank=True, null=True)
    navname = models.CharField(max_length=254, blank=True, null=True)
    navcustadr = models.CharField(max_length=254, blank=True, null=True)
    navdebt = models.FloatField(blank=True, null=True)
    navpropcode = models.CharField(max_length=254, blank=True, null=True)
    navdepcode = models.CharField(max_length=254, blank=True, null=True)
    navphoneno = models.CharField(max_length=254, blank=True, null=True)
    navreasoncomp = models.CharField(max_length=254, blank=True, null=True)
    nightshift = models.BooleanField(default=False)
    priority = models.BigIntegerField(blank=True, null=True)
    stid = models.BigIntegerField(blank=True, null=True)
    mapurl = models.CharField(max_length=254, blank=True, null=True)

    def __unicode__(self):
        return self.Id

    def save(self, *args, **kwargs):
        super(w_orders, self).save(*args, **kwargs)
        newob = tobjects()
        ...
        newob.save()



class tobjects(models.Model): 
    oid = models.BigAutoField(primary_key=True)
    wid = models.ForeignKey(w_orders, on_delete=models.CASCADE)
    objtypegisid = models.BigIntegerField(blank=True, null=True, default=1)
    objgisid = models.BigIntegerField(blank=True, null=True, default=1)
    condgenid = models.BigIntegerField(blank=True, null=True, default=1)
    condriskid = models.BigIntegerField(blank=True, null=True, default=1)
    condratsid = models.BigIntegerField(blank=True, null=True, default=1)
    condmhcoverid = models.BigIntegerField(blank=True, null=True, default=1)
    condmhwallid = models.BigIntegerField(blank=True, null=True, default=1)
    condpipehydrsid = models.BigIntegerField(blank=True, null=True, default=1)
    condpipehydreid = models.BigIntegerField(blank=True, null=True, default=1)
    condpipedepvolsid = models.BigIntegerField(blank=True, null=True, default=1)
    condpipedepvoleid = models.BigIntegerField(blank=True, null=True, default=1)
    condpipedepconssid = models.BigIntegerField(blank=True, null=True, default=1)
    condpipedepconseid = models.BigIntegerField(blank=True, null=True, default=1)
    txt = models.CharField(max_length=254, blank=True, null=True)

    def __unicode__(self):
        return self.oid

2 个答案:

答案 0 :(得分:0)

var ctx = new ClientContext(siteUrl,username,password);

答案 1 :(得分:-1)

for o in w_orders.objects.all():
    t = tobjects.create(wid=o)
    t.save()

bulk_create也可能更快,但仅适用于PostgreSQL(请阅读https://docs.djangoproject.com/en/dev/ref/models/querysets/#bulk-create):

tobjects.objects.bulk_create([tobjects(wid=o) for o in w_orders.objects.all()])

请考虑使用PEP 8作为您的代码。真可怕。