LPS
LPS

Reputation: 385

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.

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). The table GlobalUpdateTable only has one row, can that constitute a problem in any way?

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')))

Upvotes: 2

Views: 90

Answers (1)

Andrii Rusanov
Andrii Rusanov

Reputation: 4606

It seems you do coco.save instead of coco.save(). No error raised because you don't do anything wrong, but save method hasn't been called.

Upvotes: 5

Related Questions