If my Models look like:
class Publisher(models.Model):
pass
class Book(models.Model):
publisher = models.ForeignKey(Publisher)
class Page(models.Model):
book = models.ForeignKey(Book)
and I would like to get the queryset for Publisher
I do Publisher.object.all()
.
If then want to make sure to prefetch I can do:
Publisher.objects.all().prefetch_related('book_set')`
My questions are:
- Is there a way to do this prefetching using
select_related
or must I useprefetch_related
? - Is there a way to prefetch the
page_set
? This does not work:
Publisher.objects.all().prefetch_related('book_set', 'book_set_page_set')