很抱歉,如果这是一个糟糕的名称,但我不知道如何解释这个。我想创建一个表单以进入队长模型,但是需要有一个 queryset 过滤器,因此您只能从用户的 FantasyTeam <中选择玩家/强>
我的模型如下:
class Player(models.Model):
player_id = models.IntegerField(primary_key=True)
team = models.ForeignKey(Team)
player_name = models.CharField(max_length=140)
position = models.CharField(max_length=10)
first_name = models.CharField(max_length=70)
last_name = models.CharField(max_length=70)
known_name = models.CharField(max_length=70,blank=True)
birth_date = models.DateField('birth date')
weight = models.IntegerField()
height = models.IntegerField()
jersey_num = models.IntegerField()
real_position = models.CharField(max_length=25)
join_date = models.DateField('join date')
country = models.CharField(max_length=30)
value = models.DecimalField(max_digits=3,decimal_places=1)
is_active = models.BooleanField(default=False)
def __str__(self):
return '%s (%s)' % (self.player_name,self.value)
class FantasyTeam(models.Model):
FANTASY_TEAM_ID = models.AutoField(primary_key=True)
team_name = models.CharField(max_length=25)
player1 = models.ForeignKey(Player, related_name='csplayer1')
player2 = models.ForeignKey(Player, related_name='csplayer2')
player3 = models.ForeignKey(Player, related_name='csplayer3')
player4 = models.ForeignKey(Player, related_name='csplayer4')
player5 = models.ForeignKey(Player, related_name='csplayer5')
player6 = models.ForeignKey(Player, related_name='csplayer6')
player7 = models.ForeignKey(Player, related_name='csplayer7')
player8 = models.ForeignKey(Player, related_name='csplayer8')
player9 = models.ForeignKey(Player, related_name='csplayer9')
player10 = models.ForeignKey(Player, related_name='csplayer10')
player11 = models.ForeignKey(Player, related_name='csplayer11')
player_sub_gk = models.ForeignKey(Player, related_name='cssubgk')
player_sub_1 = models.ForeignKey(Player, related_name='cssub1')
player_sub_2 = models.ForeignKey(Player, related_name='cssub2')
player_sub_3 = models.ForeignKey(Player, related_name='cssub3')
def __str__(self):
return '%s' % (self.team_name)
class Captains(models.Model):
fantasy_team = models.OneToOneField(ClassicSeasonFantasyTeam,primary_key=True)
captain = models.ForeignKey(Player, related_name='cscaptain')
vice_captain = models.ForeignKey(Player, related_name='csvicecaptain')
#Associates a user with a FantasyTeam
class ClassicSeasonUserTeam(models.Model):
CS_USER_TEAMS_ID = models.AutoField(primary_key=True)
fantasy_team = models.ForeignKey(ClassicSeasonFantasyTeam)
user = models.ForeignKey(User)
total_score = models.IntegerField(default=0)
season = models.ForeignKey(Season)
def __str__(self):
return '%s' % (self.user.username)
如何实现查询集过滤器?
forms.py
class CaptainsForm(ModelForm):
class Meta:
model = ClassicSeasonCaptains
def __init__(self,*args,**kwargs):
super(ClassicSeasonCaptains,self).__init__(*args,**kwargs)
#filter to players that are only in the user's fantasy team
self.fields['captain'].queryset = ClassicSeasonFantasyTeam.objects.filter()????
self.fields['vice_captain'].queryset = ?????
答案 0 :(得分:0)
如果我理解正确,您希望限制表单中字段的选择。这个问题的答案已经给出并且非常好,请参阅:https://stackoverflow.com/a/3420588/2583290
至于Queryset过滤器本身,你想要玩家,所以从Player.objects.filter()开始。对于实际的过滤器查询,您需要先清理一下代码。由于玩家与FantasyTeam的关联方式,这将过于复杂。
考虑使用具有角色的类TeamPlayer(“普通”,“子”,“队长”)。阅读django docs
PS:避免使用复数类名。使用Captain而不是Captains。 PPS:队长应该从Player或Person类继承,而不是拥有ForeignKey