[英]Django: Not able to save to DB
I have the following view code: 我有以下查看代码:
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()
For some reason I cannot understand the first save ( variable.save
) does what is intended but the second one ( coco.save
) does not. 由于某种原因,我无法理解第一个保存(
variable.save
)是否可以实现预期的目的,而第二个保存( coco.save
)则无法实现。
If I use the following code, on the second part instead of the one above, I am able to save the value to the DB: 如果使用下面的代码,则在第二部分而不是上面的代码中,我可以将值保存到数据库:
GlobalUpdateTable.objects.all().update(variable_updated=time_now)
Both codes should be able to update the column ( variable_updated
). 两种代码都应该能够更新列(
variable_updated
)。 The table GlobalUpdateTable only has one row, can that constitute a problem in any way? 表GlobalUpdateTable只有一行,那可以以任何方式构成问题吗?
For reference I indicate the models: 作为参考,我指出了这些模型:
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')))
It seems you do coco.save instead of coco.save(). 看来您做的是coco.save而不是coco.save()。 No error raised because you don't do anything wrong, but save method hasn't been called.
没有发生错误,因为您没有做任何错误,但是尚未调用save方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.