views:

42

answers:

2
+1  Q: 

Object wont update

Hay all, my object doesnt seem to update when i call the save() method

heres my code

    car = Car.objects.get(pk=car_id)
    car.views += 1
    car.save()

and the model

views = models.FloatField(max_length=1000)

I do have a save() override method, could this cause a problem?

def save(self):
    d = timedelta(days=self.expires_in)
    if not self.id:
        self.expires_on = datetime.now() + d
        super(Car, self).save()
+5  A: 

You have an error in your code. It calls the superclasses save method only in case the object doesn't have an id yet. Fixed your code:

def save(self):
    d = timedelta(days=self.expires_in)
    if not self.id:
        self.expires_on = datetime.now() + d
    super(Car, self).save() # <-- here
Till Backhaus
+1 but check the docs for overriding save specifically:def save(self, *args, **kwargs):super(Car, self).save(*args, **kwargs)From the docs:It's also important that you pass through the arguments that can be passed to the model method -- that's what the *args, **kwargs bit does.http://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methods
DrBloodmoney
+1  A: 

Right now, it seems like your code will not go into the "if" block (unless the id is None), so the superclass's "save" method isn't getting called.

Andrew