mju*_*juk 28 python mysql django django-queryset django-database
我正在尝试创建一个使用OuterRef的非常简单的子查询(不是为了实际目的,只是为了让它工作),但仍然遇到同样的错误.
文章/ models.py
from django.db import models
class Tag(models.Model):
name = models.CharField(max_length=120)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=120)
tags = models.ManyToManyField(Tag)
def __str__(self):
return self.title
Run Code Online (Sandbox Code Playgroud)
manage.py shell代码
>>> from django.db.models import OuterRef, Subquery
>>> from posts.models import Tag, Post
>>> tag1 = Tag.objects.create(name='tag1')
>>> post1 = Post.objects.create(title='post1')
>>> post1.tags.add(tag1)
>>> Tag.objects.filter(post=post1.pk)
<QuerySet [<Tag: tag1>]>
>>> tags_list = Tag.objects.filter(post=OuterRef('pk'))
>>> Post.objects.annotate(count=Subquery(tags_list.count()))
Run Code Online (Sandbox Code Playgroud)
最后两行应该为每个Post对象提供标签数量.在这里我一直得到同样的错误:
ValueError: This queryset contains a reference to an outer query and may only be used in a subquery.
Run Code Online (Sandbox Code Playgroud)
Tod*_*dor 58
您的示例的一个问题是您不能queryset.count()用作子查询,因为.count()尝试评估查询集并返回计数.
所以人们可能会认为正确的方法是使用Count().也许是这样的:
Post.objects.annotate(
count=Count(Tag.objects.filter(post=OuterRef('pk')))
)
Run Code Online (Sandbox Code Playgroud)
这不会有两个原因:
查询集Tag选择所有Tag字段,而Count只能依赖一个字段.因此:Tag.objects.filter(post=OuterRef('pk')).only('pk')需要(选择计数tag.pk).
Count本身不是一个Subquery阶级,Count是一个Aggregate.因此生成的表达式Count不被识别为a Subquery,我们可以通过使用来解决这个问题OuterRef.
对1)和2)的应用修复将产生:
Post.objects.annotate(
count=Count(Subquery(Tag.objects.filter(post=OuterRef('pk')).only('pk')))
)
Run Code Online (Sandbox Code Playgroud)
但是, 如果您检查正在生成的查询
SELECT
"tests_post"."id",
"tests_post"."title",
COUNT((SELECT U0."id"
FROM "tests_tag" U0
INNER JOIN "tests_post_tags" U1 ON (U0."id" = U1."tag_id")
WHERE U1."post_id" = ("tests_post"."id"))
) AS "count"
FROM "tests_post"
GROUP BY
"tests_post"."id",
"tests_post"."title"
Run Code Online (Sandbox Code Playgroud)
您可能会注意到我们有一个Subquery条款.这是因为Count是一个Aggregate,现在它不会影响结果,但在其他一些情况下它可能会影响结果.这就是为什么文档提出了一些不同的方法,其中聚合被移动到GROUP BY通过subquery+ values+ 的特定组合annotate
Post.objects.annotate(
count=Subquery(
Tag.objects.filter(post=OuterRef('pk'))
# The first .values call defines our GROUP BY clause
# Its important to have a filtration on every field defined here
# Otherwise you will have more than one group per row!!!
# This will lead to subqueries to return more than one row!
# But they are not allowed to do that!
# In our example we group only by post
# and we filter by post via OuterRef
.values('post')
# Here we say: count how many rows we have per group
.annotate(count=Count('pk'))
# Here we say: return only the count
.values('count')
)
)
Run Code Online (Sandbox Code Playgroud)
最后这会产生:
SELECT
"tests_post"."id",
"tests_post"."title",
(SELECT COUNT(U0."id") AS "count"
FROM "tests_tag" U0
INNER JOIN "tests_post_tags" U1 ON (U0."id" = U1."tag_id")
WHERE U1."post_id" = ("tests_post"."id")
GROUP BY U1."post_id"
) AS "count"
FROM "tests_post"
Run Code Online (Sandbox Code Playgroud)
小智 5
django -sql-utils包使这种子查询聚合变得简单。就pip install django-sql-utils在那时:
from sql_util.utils import SubqueryCount
posts = Post.objects.annotate(
tag_count=SubqueryCount('tag'))
Run Code Online (Sandbox Code Playgroud)
SubqueryCount 的 API 与 Count 相同,但它在 SQL 中生成子查询,而不是连接到相关表。
| 归档时间: |
|
| 查看次数: |
10009 次 |
| 最近记录: |