In django I have to models A and B, B has FK reference to A.
A2B is one-to-many
In my controller code I select list of A according some criteria and this list is transferred to a template to generate html page.
This template uses FOR to generate list: {% for a in list_of_a %}
The question is that with every “a” printed I have to print related Bs as well. With non django code it would be simple join.
But how can I implement this in django?
thanks
Your question is vague, but the gist of it seems to be that you want to avoid the 1*N queries generated when looping through your
As.In Django 1.4, you can use the new
prefetch_related, which will still generate an extra query, but just one instead of 1*N.Previous versions of Django have nothing similar, but there’s an app called django-batch-select that can be used to gain roughly equivalent functionality.