I have a model with several foreign key fields, for example. the Product with Fields model, level, color, intensity (just a general example).
Then I have a page for editing all products of a certain type using a type form with products in the form of an integrated set of forms with the ability to add additional inline products using extra=10 .
The thing that I find very strange is that every time I display one of the foreign key selection fields on the template, Django queries the database to get the parameters (every time).
For example:
{% for form in formset %} {{ form.level }} {{ form.color }} {{ form.intensity }} {% endfor %}
In 20 products (and 10 empty optional forms), the above code produces 30 select * from ... of the level, color and intensity, for a total of 90 queries (shown using the Django Debug toolbar), where 3 should be sufficient. The parameters are unlikely to change the average query, but even if they did, I definitely would not want some new added options to be displayed only in the last 5 formats.
Is there a way to optimize my models / forms / views / templates so that the database doesn't clog so unnecessarily?
-
Disclaimer: I'm relatively new to django and python and can't help but think that there should be a way to somehow solve this problem.
davur
source share