I noticed, with debug_tool_bar, that a query is being made for each
element of each foreign key select option. Seems like it ought not
I am using
classes = get_object_or_404(Classes, pk=pk)
to load the record. I tried
Classes.on_site.select_related().get(pk=pk) without any change.
The question is, is this to be expected?
I'm using a fairly plain form:
{% extends 'district/district_edit_base.html' %}
{% block title %}
{{block.super}}
{% endblock title %}
{% block extra_ready_js %}
{{block.super}}
$("#district_list").collapse('show');
{% endblock extra_ready_js %}
{% block content %}
<form id="simple_form" method="post"
action="/staff/admin/classes/save/{{ id }}" class="form-horizontal">
{% include "simple_fields.html" %}
</form>
{% endblock content %}
simple_fields is
<fieldset xmlns="http://www.w3.org/1999/html">
{% csrf_token %}
{% for field in form %}
<div class="control-group {% if field.errors %} error {% endif%}">
<label class="control-label"
for="id_{{field.name}}">{{field.label}}</label>
<div class="controls">
{{field}} <span class="field_help"> {{ field.help_text }}</span>
<span class="help-inline">
{{ field.errors }}
</span>
</div>
</div>
{% endfor %}
<div id="submitButton" class="form-actions">
{% if id != 0 %}
<input type="hidden" name="Delete" id="real_delete" value="" />
<input type="button" name="DeleteInit" id="DeleteInit"
value="Delete" class="btn btn-warning btn-delete" data-toggle="modal"
data-target="#modal-delete" data-backdrop="true" data-keyboard="true"
/>
{% endif %}
<span class="spacer"> </span>
<input type="submit" name="Submit" value="Save" class="btn btn-primary" />
<span class="spacer"> </span>
<input type="button" name="return" id="return_to_list"
value="Return" title="Return to the list." class="btn" />
</div>
</fieldset>
Yes. How else would it get the potential values that can be selected?
If you want to load all in one query, you can use select_related(). If
select_related() doesn't help, then your foreign keys are probably
nullable, which are not automatically loaded with select_related().
FYI, having each foreign key lookup as a separate query may not be a bad thing.
If the cardinality of the foreign keys doesn't change much (you don't
add frequently add potential new values for that foreign key), then it
is highly likely that those queries would be served from the query
cache.
If you are displaying 1 million different forms, but all their foreign
key lookups can be served from cache, do you think it would be quicker
to do 1 million queries with 5 joins in each query, with none of the
queries cached, or 5 million queries, 1 million with no joins and 4
million served from the query cache?
Cheers
Tom