I have the following table structure:
menu --{ page --{ title
From the menu i would like to get fetch related pages. Pages also have related titles.
The following code works exactly as i would like however the for loop generates an individual query each iteration.
The prefetch_related in the initial query doesn’t seem to fetch the title. Is there a better way to do this resulting in a smaller number of queries?
menu_data = MenuOrder.objects \
.filter(menu__name=kwargs['menu']) \
.filter(page__title_set__language=language) \
.prefetch_related('page') \
.order_by('priority')
for menu_obj in menu_data:
title_obj = menu_obj.page.title_set.get()
Thanks in advance,
Well, i found out that reversing the relationship has reduced the number of queries by a large number:
I don’t know if there is a better method, so for now i will accept this one. Thanks to everyone that took a look..