Having these kind of model definitions and a relationship between the two:
class Car(models.Model):
description = models.CharField(max_length=35)
def save(self, **kwargs):
invalidate_cache()
super().save(**kwargs)
def delete(self, **kwargs):
invalidate_cache()
return super().delete(**kwargs)
class Passenger(models.Model):
car = models.ForeignKey(Car, related_name='passengers')
I have defined custom save
and delete
on the Car model because when a car instance is modified I have to perform some extra operations, in particular I need to invalidate a cache.
My doubt is: creating/updating/deleting a related model would call these custom methods?
I'll try to be more clear:
c1 = Car(description='super fast car')
p = Passenger(car=c1)
Clearly the creation of the c1
calls the Car.save
but would the creation of the instance p
of Passenger call the Car.save
or not?
From my tests it seems so but I want to be more sure it wasn't just a specific case and this happens all the time in the Django model handling cycle (I could not find a specific documentation on this).