0

Sorry if it's a bad name for a title, but I'm not sure how to explain this. I would like to make a form for entry into the Captains model, however there needs to be a queryset filter so you can only select players from a user's FantasyTeam.

I have the models as follows:

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)

How can I achieve the queryset filter?

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 = ?????
Matchday
  • 619
  • 1
  • 10
  • 17

1 Answers1

0

If I understand correctly you want to limit the choices for a field in the form. The answer to that question is already given and is really good, see: https://stackoverflow.com/a/3420588/2583290

As for the Queryset filter itself, you want Players, so start with Player.objects.filter(). For the actual filter query you'll need to clean your code a bit first. It's would be overly complex because of how Players are linked to your FantasyTeam.

Consider having A class TeamPlayer with a role ("normal", "sub", "captain"). Read up in the django docs

PS: Avoid using plural class names. Use Captain instead of Captains. PPS: Captain(s) should probably inherit from Player or a Person class rather than have a ForeignKey

Community
  • 1
  • 1
rrmoelker
  • 152
  • 10
  • Thanks for the advice. Without changing the model structure, is there a possibility of filtering players that are in a fantasy team? I am still finding it difficult after looking at the answer to the other question you posted – Matchday Sep 30 '14 at 14:59