我想要做的就是拉出所有拥有12名以上球员的球队。我被告知我需要使用带有计数的.annotate来完成它。我正在查看https://docs.djangoproject.com/en/1.10/topics/db/aggregation/但不明白如何对其进行查询并将其放入html中。我假设拉一个查询,然后做html使它做正确的计数。
指派的问题是......显示所有拥有12名或更多球员的球队,过去和现在。 (提示:查看Django注释函数。)
views.py
from django.shortcuts import render, redirect
from .models import League, Team, Player
from . import team_maker
def index(request):
context = {
"leagues": League.objects.all(),
"teams": Team.objects.all(),
"players": Player.objects.all(),
"baseball" : League.objects.filter(sport__contains="Baseball"),
"women" : League.objects.filter(name__contains="Womens'"),
"ice" : League.objects.filter(sport__contains="ice"),
"nofootball" : League.objects.exclude(sport__contains="football"),
"conferences" : League.objects.filter(name__contains="conference"),
"atlantic": League.objects.filter(name__contains="Atlantic"),
"teamdallas": Team.objects.filter(location__contains="Dallas"),
"raptor": Team.objects.filter(team_name__contains="Raptor"),
"cityloc": Team.objects.filter(location__contains="city"),
"startT": Team.objects.filter(team_name__startswith="T"),
"abc": Team.objects.order_by('location'),
"cba": Team.objects.order_by('location').reverse(),
"cooper": Player.objects.filter(last_name="Cooper"),
"joshua": Player.objects.filter(first_name="Joshua"),
"nocooper": Player.objects.filter(last_name="Cooper").exclude(first_name="Joshua"),
"wyatt": Player.objects.filter(first_name="Alexander")|Player.objects.filter(first_name="Wyatt"),
"atlanticsoccer" : Team.objects.filter(league__name__contains='atlantic', league__sport__contains="soccer"),
"bostonp" : Player.objects.filter(curr_team__team_name__contains='Penguins'),
"icbc" : Player.objects.filter(curr_team__league__name__contains='International Collegiate Baseball '),
"footballlopez" : Player.objects.filter(curr_team__league__name__contains="American Conference of Amateur Football", last_name__contains="lopez"),
"footballplayer" : Player.objects.filter(curr_team__league__sport__contains="football"),
"sophia" : Team.objects.filter(curr_players__first_name__contains='sophia'),
"sophiale" :League.objects.filter(teams__curr_players__first_name__contains='sophia'),
"notrough" :Player.objects.filter(last_name__contains='Flores').exclude(curr_team__team_name__contains='rough'),
"sevans" :Team.objects.filter(all_players__first_name__contains='Samuel'),
"Tiger" :Player.objects.filter(all_teams__team_name__contains='Tiger'),
"vikings":Player.objects.filter(all_teams__team_name__contains='vikings'),
"jgray":Team.objects.filter(all_players__first_name__contains='Jacob').exclude(team_name__contains='colts'),
"joshua":Player.objects.filter(all_teams__league__name__contains='Atlantic Federation of Amateur Baseball')&Player.objects.filter(first_name='Joshua'),
"num12":Player.objects.annotate(num13=)
}
return render(request, "leagues/index.html", context)
def make_data(request):
team_maker.gen_leagues(10)
team_maker.gen_teams(50)
team_maker.gen_players(200)
return redirect("index")
models.py
from django.db import models
class League(models.Model):
name = models.CharField(max_length=50)
sport = models.CharField(max_length=15)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Team(models.Model):
location = models.CharField(max_length=50)
team_name = models.CharField(max_length=50)
league = models.ForeignKey(League, related_name="teams")
class Player(models.Model):
first_name = models.CharField(max_length=15)
last_name = models.CharField(max_length=15)
curr_team = models.ForeignKey(Team, related_name="curr_players")
all_teams = models.ManyToManyField(Team, related_name="all_players")
答案 0 :(得分:1)
Team.objects.annotate(c=Count('all_players')).filter(c__gt=12')
这样做会是:
在Team
模型/表中的每个记录/行中,它将附加/注释新值。 c
一个。{1}}。您可以随意命名。此c
将是与此player
/ row
相关联的record
的数量。
然后,由于c
值在所有行中都有注释,因此它会过滤这些值并获取c
大于12(player
s)的所有行。< / p>