Reputation: 423
Should I constantly use Django select_related or prefetch_related every time when I use models with OneToMany relations?
If I have multiple foreign keys. Can I use it, like this?
class A(models.Model):
pass
class B(models.Model):
pass
class C(models.Model):
a = models.ForeignKey(A)
b = models.ForeignKey(B)
# example usage
for entry in C.objects.all().select_related('a').select_related('b'):
pass
Upvotes: 1
Views: 133
Reputation: 832
You could also use it like this:
for entry in C.objects.select_related('a', 'b').all():
pass
And you should use it only when you want to get the foreign keys to make operations with them in another case you shouldn't.
Upvotes: 2