I have a model, for instance
class ModelA(models.Model):
name = models.CharField()
class ModelB(models.Model):
last_modified_time = models.DateTimeField()
as = models.ManyToManyField('ModelA')
when I delete an instance of ModelA I would like to also update the last_modified_time field on all ModelB instances that referred to ModelA. I can use a predelete signal for this, i.e.
def pre_delete_handler(sender=None, instance=None, *args, **kwargs):
# update all using instance.modelbs...
models.signals.pre_delete.connect(pre_delete_handler, sender=ModelA)
but then every time a new model references it via a ManyToMany this bit of code needs to be updated which isn't the best from a maintenance point of view. I am looking for a way to enumerate all models that ModelA is referenced by so I can have a single update that covers all cases but can't figure out the best way to do this. What is the proper way to enumerate all objects that ModelA is related to?