开发者

{% with %} templatetag to avoid query execution multiple times?

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 : )

{% with %} templatetag to avoid query execution multiple times?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜