Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django subquery using QuerySet

Is it possible to perform a subquery on a QuerySet using another QuerySet?

For example:

q = Something.objects.filter(x=y).extra(where=query_set2)
like image 952
epoch Avatar asked May 13 '26 19:05

epoch


1 Answers

Short answer: No. The extra method doesn't expect querysets to be passed in.

If you think about it a bit, it makes sense. Querysets are an abstraction used to represent the results of a fetch operation on the database and extra is a convenient way of attaching custom fields from the database to a queryset. Unless you change the fundamental nature of extra to mean "custom filtering with another queryset" this will not work.

like image 53
Manoj Govindan Avatar answered May 15 '26 09:05

Manoj Govindan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!