django - Django 反向关系(带注释和过滤)
问题描述
我想在特定比赛的“MatchSup”表中获取少于 5 条记录的玩家
Player.objects.filter('age="25"',matchsup__match__in='matchlist').annotate(numviews=Count('matchsup__player__lt=5',)) 像这样(我知道这是错误的)
class Player(models.Model):
name= models.CharField(max_length=64, null=True, blank=True)
age= ...
class MatchSup(models.Model):
player= models.ForeignKey(Player, null=True, blank=True, on_delete=models.SET_NULL)
match = models.ForeignKey('Match', on_delete=models.CASCADE)
解决方案
annotate
模型上的计数和计数上的Player
过滤器:
from django.db.models import Count
Player.objects.filter(
age=25,
matchsup__match__in=matchlist
).annotate(
match_count=Count('matchsup')
).filter(match_count__lt=5)
推荐阅读
- html - 如何在 4 列中显示我的自定义 woocommerce 产品?
- dart - Flutter:无法从 Firebase 身份验证中获取错误代码?
- javascript - FormData 无法发送 Javascript
- python - 错误:找不到 MagickWand 共享库。您可能还没有安装 ImageMagick 库。(虽然我有)
- angular - 从 typescript angular 6 中选择默认选项值
- c# - DataGridViewCell 在更改焦点之前不会更新
- postgresql - 仅返回 RANK() 之后的最小值
- git - 无法使用 Jenkins 配置 Selenium
- java - 使用 Spring AMQP 接收和发送 Java 对象
- php - 并非所有子类别都来自 codeigniter 中的模型