#When to use an explicit Subquery?

3 messages · Page 1 of 1 (latest)

frosty plaza
#

I've gotten by for a long time writing subqueries without wrapping them in models.Subquery.
In the documentation it says: "The examples in this section are designed to show how to force Django to execute a subquery. In some cases it may be possible to write an equivalent queryset that performs the same task more clearly or efficiently."
https://docs.djangoproject.com/en/5.0/ref/models/expressions/#subquery-expressions

When should I make explicit subqueries? Are there some general guidelines I can follow?

signal jungle
#

🤷 when it's the best way to get the results you want. "Best" includes "efficient" and "easy to understand". Yes, those two things can be in tension. Yes, this is a mostly-useless answer.

#

This is why programmers get paid a lot of $