3

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)
1
  • 1
    What are you really trying to do here? The code makes no sense to me. Do you really want to do query_set2.filter(something__x=y)? Commented Sep 22, 2010 at 13:02

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

I may understand your question in two ways.

  1. You can specify multiple variables in your filter parameters, for example :

    q = Something.objects.filter(x=y, w=z)
    
  2. You want to make what is called a "join" in SQL. This can be done via the aggregation system of Django, see the official Django Official Documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.