3

I have 2 models

class A(models.Model):
    val = models.IntegerField()

class B(models.Model):
    val2 = models.IntegerField()
    a = models.ForeignKey(A)

class C(models.Model):
    b = models.ForeignKey(B)
    val3 = models.IntegerField()

How is the query -

C.objects.select_related('B').all()

better than -

C.objects.select_related('B__val2').all()

And if not how query one can be optimised?

Pankhuri Agarwal
  • 764
  • 3
  • 23

1 Answers1

0

Try to filter model you need, by lowcase filter of the child models

B.objects.filter(c__isnull=False)

read more here lookups-that-span-relationships

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
  • Filtering is another way to do it. But if referencing the model's field could be used for that in some way than that would be better. – Pankhuri Agarwal Sep 14 '17 at 09:04