Reputation: 952
Do template value tags force django to hit the database when called against a non-context value?
For example:
{{ request.user.username }}
Is the call to show the currently logged in user's username. However, something like {{ request.user.someobject_set.all }}
will dump a FK traversed queryset into the template.
Does the user's someobject
set get dumped into context by default, or do I need to make a context entry with a queryset in the def get_context_data
of my view? And by extension, any other, non-request object that may be found by association?
The below doc shows when the querysets are evaluated in raw python, but doesn't really mention templates and views and their relationship.
https://docs.djangoproject.com/en/1.6/ref/models/querysets/#when-querysets-are-evaluated
Upvotes: 6
Views: 970
Reputation: 599610
Evaluating things in the template is exactly the same as evaluating them anywhere else. When the template is rendered, the variables will be resolved, and if the object referred to requires a database lookup, then one will be carried out by that object. But this isn't the template doing anything clever, it's just telling request.user
to fetch its someobject_set
attribute and then calling all
on it which is exactly the same as would happen in Python code.
Upvotes: 9