Django:无法保存到DB

时间:2016-02-25 12:44:14

标签: python django

我有以下视图代码:

def control_activation(request, device_id, variable_name, activated):

    time_now = int(datetime.utcnow().strftime('%s'))

    variable_qs = Variables.objects.filter(device_id=device_id, name=variable_name)
    variable = variable_qs[0]
    variable.activation = activated
    variable.updated_at = time_now
    variable.save()

    coco_qs = GlobalUpdateTable.objects.all()
    coco = coco_qs[0]
    coco.variable_udated = time_now
    coco.save

    return HttpResponse()

出于某种原因,我无法理解第一次保存(variable.save)是做了什么,但第二次保存(coco.save)没有。

如果我使用以下代码,在第二部分而不是上面的部分,我可以将值保存到数据库:

GlobalUpdateTable.objects.all().update(variable_updated=time_now)

两个代码都应该能够更新列(variable_updated)。表GlobalUpdateTable只有一行,可以以任何方式构成问题吗?

作为参考,我指出了模型:

class Variables(models.Model):

    name = models.CharField(max_length=20)
    device_id = models.ForeignKey(Devices, to_field='id')
    device_addr = models.CharField(max_length=6)
    device_type = models.CharField(max_length=20)
    response_tag = models.CharField(max_length=10)
    command = models.CharField(max_length=6)
    config_parameter = models.CharField(max_length=6)
    unit = models.CharField(max_length=4)
    direction = models.CharField(max_length=6)
    period = models.IntegerField(null=True, blank=True, default=900)
    activation = models.BooleanField(default=False)
    formula = models.TextField(null=True, blank=True)
    variable_uuid = models.CharField(max_length=36, primary_key=True)
    mapping = models.TextField(null=True, blank=True)
    updated_at = models.BigIntegerField(default=int(datetime.utcnow().strftime('%s')))
    created_at = models.BigIntegerField(default=int(datetime.utcnow().strftime('%s')))

def __unicode__(self):
    return unicode(self.device_id) + '_' + unicode(self.name)


class GlobalUpdateTable(models.Model):
    device_updated = models.BigIntegerField(default=int(datetime.utcnow().strftime('%s')))
    variable_updated = models.BigIntegerField(default=int(datetime.utcnow().strftime('%s')))

1 个答案:

答案 0 :(得分:5)

看来你做的是coco.save而不是coco.save()。没有引起错误,因为你没有做错任何事,但是没有调用save方法。