Yeah, that's expected behavior.
settings = SomeModel.objects.all()
for e in settings:
print e.pk
for e in settings:
print e.pk
for e in settings:
print e.pk
In the above, the database will be hit only once but......
for e in SomeModel.objects.all():
print e.pk
for e in SomeModel.objects.all():
print e.pk
for e in SomeModel.objects.all():
print e.pk
....the database will be hit three times
Sent from my Windows Phone
------------------------------
From: Venkatraman S
Sent: 3/28/2013 3:51 AM
To: [email protected]
Subject: No multiple calls while iterating the queryset in template
Interesting Observation: So, am passing a queryset to my template and based
on the way a field in the record 'starts-with', i am rendering the record
into different 'tables' in the view.
Something like:
{% for record in settings %}
{%if record.type|starts_with:"EMAIL_" %}
<tr> ...show record details .. </tr>
{% endif %}
{% endfor %}
{% for record in settings %}
{%if record.type|starts_with:"WORKSPACE_" %}
<tr> ...show record details .. </tr>
{% endif %}
{% endfor %}
{% for record in settings %}
{%if record.type|starts_with:"DASHBOARD_" %}
<tr> ...show record details .. </tr>
{% endif %}
{% endfor %}
I was worried that passing over the queryset in different regions in the
'same' template might cause multiple(in this case 3) different queries to
the DB. But DDT shows me just one query to the DB :)
Regards,
Venkat
@venkasub
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.