I noticed that I get too many database hits whilst rendering some complicated pages with a lot of related entities lookups. I was able to partially optimize it by using select_related() where possible in the views but there is another portion of hits that I get from condition check and perhaps there is something I overlooked in the docs that could help.
I used django-debug-toolbar to learn that this expression will give me two database hits – one to check the entry exists and another one to actually render it. The reason I do this lookup is because I don’t want empty “()” all over the place. Is there a template tag or something that I could use to surround the value by something?
{% if project__current_phase__recent_status__comment %}
({{ project__current_phase__recent_status__comment }})
{% endif %}
The with template tag. From the docs:
Example: