英文:
How to run a query with an inner query in it in Django?
问题
I'm trying to run a query with an inner query in it as shown below:
# 内部查询
Product.objects.filter(category__in=Category.objects.get(pk=1))
But, I got the error below:
> TypeError: 'Category' object is not iterable
Also, I'm trying to run a query with an inner query in it as shown below:
# 内部查询
Product.objects.filter(category=Category.objects.filter(pk=1))
But, I got the error below:
> ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
So, how can I run these queries above with inner queries in them?
英文:
I'm trying to run a query with an inner query in it as shown below:
# Inner query
Product.objects.filter(category__in=Category.objects.get(pk=1))
But, I got the error below:
> TypeError: 'Category' object is not iterable
Also, I'm trying to run a query with an inner query in it as shown below:
# Inner query
Product.objects.filter(category=Category.objects.filter(pk=1))
But, I got the error below:
> ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
So, how can I run these queries above with inner queries in them?
答案1
得分: 1
To category__in, you need to assign the queryset made by filter() instead of the object made by get() as shown below:
# 这里
Product.objects.filter(category__in=Category.objects.filter(pk=1))
And, the code below is equivalent to the code above:
qs = Category.objects.filter(pk=1)
Product.objects.filter(category__in=qs)
And, one SELECT
query with WHERE IN
below is run according to the PostgreSQL query logs below. *You can see my answer explaining about how to log queries in PostgreSQL:
Also to category
, you need to assign the object made by get()
instead of the queryset made by filter()
as shown below:
# 这里
Product.objects.filter(category=Category.objects.get(pk=1))
And, the code below is equivalent to the code above:
obj = Category.objects.get(pk=1)
Product.objects.filter(category=obj)
And, two SELECT
queries with WHERE
below are run according to the PostgreSQL query logs below:
英文:
To category__in, you need to assign the queryset made by filter() instead of the object made by get() as shown below:
# Here
Product.objects.filter(category__in=Category.objects.filter(pk=1))
And, the code below is equivalent to the code above:
qs = Category.objects.filter(pk=1)
Product.objects.filter(category__in=qs)
And, one SELECT
query with WHERE IN
below is run according to the PostgreSQL query logs below. *You can see my answer explaining about how to log queries in PostgreSQL:
Also to category
, you need to assign the object made by get()
instead of the queryset made by filter()
as shown below:
# Here
Product.objects.filter(category=Category.objects.get(pk=1))
And, the code below is equivalent to the code above:
obj = Category.objects.get(pk=1)
Product.objects.filter(category=obj)
And, two SELECT
queries with WHERE
below are run according to the PostgreSQL query logs below:
答案2
得分: 1
Eaiser is to make a simple JOIN:
Product.objects.filter(category_id=1)
For some databases it will not matter, but MySQL is notably not very good with subqueries, so the JOIN version will outperform the subquery one.
英文:
Eaiser is to make a simple JOIN:
<pre><code>Product.objects.filter(<b>category_id=1</b>)</code></pre>
For some databases it will not matter, but MySQL is notably not very good with subqueries, so the JOIN version will outperform the subquery one.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论