Answer the question
In order to leave comments, you need to log in
How to get all objects linked via ForignKey from QuerySet of linked object model?
There was a problem with the speed of executing queries to database tables due to the strong nesting of objects.
Models:
class Model1(models.Model):
field = models.CharField(max_lenght=100)
class Model2(models.Model):
field = models.ForignKey(Model1, 'model_1', related_name='model_1', on_delete=models.CASCADE)
class Model3(models.Model):
field = models.ForignKey(Model2, 'model_2', related_name='model_2', on_delete=models.CASCADE)
Model3.objects.filter(model_2__model_1_id__in=Model1.objects.filter(.....).values('id'))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question