class ModelA(Model):
pass
class ModelB(Model):
modela = ForeignKey(ModelA, on_delete=CASCADE)
class ModelC(Model):
modelb = ForeignKey(ModelB, on_delete_CASCADE)
In this case Django will automatically create a few additional managers for us. For example, if ma
is an instance of ModelA
, then we can write: ma.modelb_set.all()
, and if mb
is an instance of ModelB
, then we can write: mb.modelc_set.all()
.
It would be convenient to be able to reference all ModelC
instances related to ModelA
. Of course, if ma
is an instance of ModelA
, we can always write: ModelC.objects.filter(modelb__modela=ma).distinct()
. However, it would be more convenient if we could define a manager for ModelA
that would allow us to execute the above query by simply writing: ma.modelc_set.all()
How to write such a manager for ModelA
?