我正在从学校开展一个项目,其中一个问题是列出一个名为" Sophia"的(当前)球员的所有联赛。我已经能够获得所有联赛时期,所有球员都称为索菲亚。我怎么似乎无法弄清楚如何从最后一个表开始,其中第一个名称引用第一个表,联盟将获得我需要的答案。我希望有人能看到我遇到的问题。我已将下面的模型和视图联系起来。
Manage.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")
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" : Team.objects.filter(league__contains='sophia'),
}
print Team.objects.filter(league__contains='sophia')
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")
index.html just the part you need
<h5>Question 8</h5>
{% for player in sophiale %}
<li>{{player.name}}</li>
{% endfor %}
</ol>
答案 0 :(得分:2)
试试这个:
League.objects.filter(teams__ all_players__first_name__contains='sophia')