我正在尝试为登录的玩家打印一个灯具列表,他们可以查看的灯具只是他们所属团队的灯具。基本上我想在userTeams中输出hometeamID / awayteamID =每个teamID的灯具,其中userID = request.user。
灯具型号:
class Fixtures(models.Model):
fixture = models.CharField(max_length=100)
hometeamID =
models.ForeignKey(Team,related_name='team1',on_delete=models.CASCADE)
awayteamID =
models.ForeignKey(Team,related_name='team2',on_delete=models.CASCADE)
datetime = models.DateTimeField()
homegoals = models.PositiveSmallIntegerField()
awaygoals = models.PositiveSmallIntegerField()
played = models.NullBooleanField()
UserTeams模型:
class UserTeams(models.Model):
userID = models.ForeignKey(User,on_delete=models.CASCADE)
teamID = models.ForeignKey(Team,on_delete=models.CASCADE)
赛程视图:
def fixturesview(request):
query = Fixtures.objects.all()
return render(request, 'teammanager/fixtures.html', {
"fixtures": query
})
HTML:
<p>Fixtures</p>
{%for team in teams%}
<h3>{{fixtures.fixture}}</h3>
{%endfor%}
答案 0 :(得分:0)
如何 - 通过
为用户寻找团队teams = UserTeams.objects.values_list('teamID', flat=True).filter(userId=user)
然后在
中使用它fixtures = Fixtures.objects.filter(Q(hometeamID__in=teams) | Q(awayteamID__in=teams))
但是,恕我直言,这些外国字段的名称不应以ID结尾。