Django call function when an object gets added
Posted
by dotty
on Stack Overflow
See other posts from Stack Overflow
or by dotty
Published on 2010-06-01T21:57:03Z
Indexed on
2010/06/01
22:03 UTC
Read the original article
Hit count: 161
Hay, i have a simple model
class Manufacturer(models.Model):
name = models.CharField()
car_count = models.IntegerField()
class Car(models.Model):
maker = ForeignKey(Manufacturer)
I want to update the car_count field when a car is added to a manufacturer, I'm aware i could just count the Manufacturer.car_set() to get the value, but i want the value to be stored within that car_count field.
How would i do this?
EDIT
Would something like this work?
def save(self):
if self.id:
car_count = self.car_set.count()
self.save()
© Stack Overflow or respective owner