Say I have the following models:
class Baz(models.Model):
winning = models.CharField(max_length=100)
class Bar(models.Model):
baz = models.ForeignKey(Baz)
class Foo(models.Model):
bar = models.ForeignKey(Bar)
Now I have an instance of Foo, foo. How many queries does the following line execute?
winning = foo.bar.baz.winning
Does it do one for each foreign-key ., or is Django smart enough to only do one query here?
That will require 3 queries.
One to get foo, bar, and baz.
Use
select_related()to get them at once.