Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11523

django select_related works in template rendering?

I'm wondering. If I use select_related on the view, will it save a database hit if I use the object on the template?

Lets say:

views.py

one_thing = things.objects.filter(...).select_related("another_thing")

template.html

<p>{{ one_thing.another_thing }}</p>

The docs about select_related write:

Returns a QuerySet that will “follow” foreign-key relationships, selecting additional related-object data when it executes its query. This is a performance booster which results in a single more complex query but means later use of foreign-key relationships won’t require database queries.

Upvotes: 1

Views: 715

Answers (1)

Shang Wang
Shang Wang

Reputation: 25539

The template code for django is executed in the backend and render the data in the template when the page finishes loading, so there is no difference whether you use it in your views.py or in your template.

Upvotes: 2

Related Questions