2014-09-30 87 views
0

對不起,如果它是一個壞名稱的標題,但我不知道如何解釋這一點。我想填寫隊長的表格,但是需要有一個查詢集過濾器,所以您只能從用戶的FantasyTeam中選擇玩家。Django - 從另一個模型中過濾對象

我有型號如下:

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

如果我理解正確的話,你要限制在形成場的選擇。這個問題的答案已經給出,並且非常好,請參閱:https://stackoverflow.com/a/3420588/2583290

至於Queryset過濾器本身,您需要Players,所以從Player.objects.filter()開始。對於實際的過濾器查詢,您需要首先清理您的代碼。由於玩家與你的FantasyTeam聯繫在一起,這會過於複雜。

考慮讓A類TeamPlayer具有角色(「正常」,「子」,「隊長」)。閱讀django docs

PS:避免使用複數類名。使用船長,而不是船長。 PPS:隊長應該可以從Player或Person類繼承而不是擁有ForeignKey

+0

感謝您的建議。在不改變模型結構的情況下,是否有可能過濾處於幻想團隊中的玩家?查看您發佈的其他問題的答案後,我仍然覺得很困難 – Matchday 2014-09-30 14:59:10

相關問題