I have 4 models and i want to retrieve a join between them
ModelA
class ModelA(models.Model):
product = models.ForeignKey(ModelB)
group = models.ForeignKey(Group)
ModelB
class ModelB(models.Model):
title = models.CharField()
ModelC
class ModelC(models.Model):
product = models.ForeignKey(ModelB)
group = models.ForeignKey(ModelD)
ModelD
class ModelD(models.Model):
name = models.CharField()
Now i want all my ModelA objects joined with ModelB, ModelC and ModelD In sql this is pretty easy thing to do. Just make joins between tables. With Django ORM i'm stuck because i only can do forward relation.
I'm doing this
ModelA.objects.all().select_related(product)
But i can't join ModelC I already have read this article, but i don't want to loop over my big list, to do a simple thing! And i want to hit database only once.
I'm using the last version of Django and i hope there is already a solution to this, that i'm not aware of.
Thank you.