Does using django querysets in templates hit the database?

2024/11/13 8:36:58

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

Answer

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.

https://en.xdnf.cn/q/72183.html

Related Q&A

how to randomly sample in 2D matrix in numpy

I have a 2d array/matrix like this, how would I randomly pick the value from this 2D matrix, for example getting value like [-62, 29.23]. I looked at the numpy.choice but it is built for 1d array.The f…

How to update figure in same window dynamically without opening and redrawing in new tab?

I am creating a 3D scatter plot based off a pandas dataframe, and then I want to re-draw it with slightly updated data whenever the user presses a button in my program. I almost have this functionality…

Serializing a C struct in Python and sending over a socket

Im trying to serializing the following C structstruct packet {int id;unsigned char *ce;unsigned char *syms; };in Python and send it over a socket. The number of elements pointed by ce and syms are know…

creating multiple audio streams of an icecast2 server using python-shout

I am trying to create a web radio server to stream 3 sources at once. I am using python to create a source client for icecast2 using the python-shout library. I am not too familiar with the language (p…

Custom Deployment to Azure Websites

I recently started using Gulp.js to package all my CSS and JavaScript into single files, which I then include in my web app. My web app is written in Python (using Flask).I obviously dont want to track…

Python - Gspread Request Error 401

Im currently making a Discord-bot that connects to a Google-spreadsheet (gspread). But after Ive been running it for a while it starts to hand out errors and it cant connect to my gspread anymore (unle…

paramiko server mode port forwarding

I need to implement a ssh server using paramiko that only handles -R port forwarding requests like this:ssh -N -T -R 40005:destination_host:22 [email protected]So far from what i understand ill have to…

Pandas dataframe.hist() change title size on subplot?

I am manipulating DataFrame using pandas, Python. My data is 10000(rows) X 20(columns) and I am visualizing it, like this.df.hist(figsize=(150,150))However, if I make figsize bigger, each of subplots t…

Regex match back to a period or start of string

Id like to match a word, then get everything before it up to the first occurance of a period or the start of the string. For example, given this string and searching for the word "regex":s = …

Finding differences between strings

I have the following function that gets a source and a modified strings, and bolds the changed words in it.def appendBoldChanges(s1, s2):"Adds <b></b> tags to words that are changed&qu…