django - 如何从 Django 中的 ManyToManyField 计算查询集?
问题描述
在模型中:
class Match(models.Model):
user = models.ManyToManyField(User, blank=True)
hot_league = models.ManyToManyField('HotLeague', blank=True)
class HotLeague(models.Model):
user = models.ManyToManyField(User, blank=True)
price_pool = models.IntegerField()
winner = models.IntegerField()
在视图中:
match = Match.objects.filter(user=request.user)
hot_league = match.hot_league.filter(user=request.user).count()
这里的意见count()
是行不通的。我如何计算位于hot_league
哪个match
?
解决方案
这里的问题是它match
不是一个对象Match
,它是一个QuerySet
包含零个、一个或多个Match
对象的对象。因此你不能使用hot_league
这样的关系QuerySet
。
如果你想计算所有属于 的HotLeague
srequest.user
并且有一个Match
属于那个request.user
的,你可以把它算作:
HotLeague.objects.filter(user=request.user, match__user=request.user).count()
如果它属于as的多个匹配项,这将多次计算相同 的值。如果你希望每个只计算一次,你可以添加一个[Django-doc]到它:HotLeague
request.user
user
HotLeague
.distinct()
HotLeague.objects.filter(user=request.user, match__user=request.user).distinct().count()
或者,您可以使用该用户的 s 数量来注释Match
es 的数量HotLeague
,例如:
from django.db.models import Count, Q
matches = Match.objects.filter(user=request.user).annotate(
nhotleagues=Count('hotleague', filter=Q(hotleague__user=request.user))
)
Match
源自该查询集的每个都将具有一个额外的属性,该属性nhotleagues
指定该HotLeague
用户的 s 数。所以你可以这样渲染:
{% for match in matches %}
{{ match.pk }}: {{ match.notleagues }}<br>
{% endfor %}
您可以总结计数,例如:
from django.db.models import Count, Q
matches = Match.objects.filter(user=request.user).annotate(
total=Count('hotleague', distinct=True, filter=Q(hotleague__user=request.user)) +
Count('mega_league', distinct=True, filter=Q(megaleague__user=request.user)) +
Count('head_to_head_league', distinct=True, filter=Q(head_to_head_league__user=request.user))
)
当然,您在 JOIN 中添加的表越多,查询的开销就越大。
推荐阅读
- javascript - 在 'https://identitytoolkit.googleapis.com/v1/accounts:/signUp? 访问 XMLHttpRequest?来自 'http://localhost:8080' 的来源已被阻止
- php - Nextcloud 数据库迁移执行
- linux - 带有 ifort 的 tracebackqq() 导致分段错误
- flutter - Flutter 中带有 ListView 的 FAN 横幅
- docker - 使用 Wireguard 容器后面的端口绑定访问 Docker 容器
- python - 使用 OpenCV 和 Python 使用范围滑块(轨迹条)创建蒙版图像
- python - 如何在 Python 中用点覆盖水平条形图?
- bash - 复制(远程)某些文件夹
- c# - 如何在实体框架中的表中的两条记录之间建立关系
- html - 如何隐藏 form.errors 直到它处于活动状态