If I use for example {{ some_model.objects.all }}
in several places in one template, will the query be executed each time? And if so, should I always use {% with some_model.objects.all as some_name %}
to avoid that? I saw this in s开发者_开发百科everal apps, so I'm wondering if I understand it correctly.
Yes, it does repeat the query. You should populate the context in your view instead of calling these functions within the template.
Using {% with %}
indeed does solve the problem though for future calls, but I still vote for the view.
PS:
I highly suggest downloading django-debug-toolbar
https://github.com/dcramer/django-debug-toolbar
You can test these ideas in a second : )
精彩评论