title: How do I store a ForeignKey list in a Django model?
body: <p>I am building a platform where the user can <em>buy</em> players (like in fantasy leagues). So I have 4 models as such :</p>
  
  <pre><code>POSITION_CHOICES = ((1,'1'),
                      (2,'2'),
                      (3,'3'),
                      (4,'4'),
                      (5,'5'),
  )
  
  class Team(models.Model):
      ...
  
  class Player(models.Model):
      team = models.ForeignKey(Team, blank=True, null=True, ...)
      position = MultiSelectField(
      user = models.ForeignKey(User, unique=True)
      player1 = models.ForeignKey(Player, related_name='+', blank=True,null=True,
                                  limit_choices_to={'position__icontains':1},...)
      player2 = ...
      player3 = ...
      player4 = ...
      player5 = ...
      ...
  
  class FantasyTeams(models.Model):
      user = models.ForeignKey(User, unique=True)
      team1 = models.ForeignKey(Team, verbose_name='Team', blank=True, null=True,
                                on_delete=models.SET_NULL)
      ...
  </code></pre>
  
  <p>Basically a <code>Team</code> contains 0 or more <code>Players</code>. Both <code>FantasyPlayers</code> and <code>FantasyTeams</code> are players and teams the user bought (he can buy 0 to 5 players and 0 to 1 team, but those numbers are likely to be changed).
  LUTION
  The current way I've done it is quite ugly even if it works. I am now looking for something neater (as I don't find this to be generic at all), something like replacing the <code>player#</code> fields by <code>players</code> which would contains a <code>ForeignKey</code> list. I am also willing to merge the 2 models, <code>FantasyPlayers</code> and <code>FantasyTeams</code>, into one which would contains a list of players (max. 5), a list of teams (max. 1) and the <code>ForeignKey</code> to its user.</p>
  
  <p>I saw that <em>one-to-many-fields</em> was mentioned several times when looking at similar questions, but all it did was confusing me even more with the <code>OneToOneField</code> and the <code>ManyToManyField</code>.</p>
  
  <p>Is there any direction you could give me to store a list of <code>ForeignKey</code> in a Django model ?</p>
  
  <hr>
  
  <h2>Solution</h2>
  
  <p>The following is replacing the previous <code>FantasyPlayers</code> and <code>FantasyTeams</code> :</p>
  
  <pre><code>class FantasyTeam(models.Model):
      user = models.ForeignKey(User, unique=True)
      players = models.ManyToManyField(Player, blank=True, null=True)
      teams = models.ManyToManyField(Team, blank=True, null=True)
      ...
  </code></pre>
  
  <p>This is now how I create and save the models (through <code>ModelForm</code>) :</p>
  
  <pre><code>def fantasy_create(request):
      # Handling form datas
      if request.method == "POST":
          form = EditFantasyTeamForm(request.POST)
          if form.is_valid():
              fantasy = FantasyTeam.objects.create(user=request.user)
              for player in form.cleaned_data['players']:
                  fantasy.players.add(player)
              for team in form.cleaned_data['teams']:
                  fantasy.teams.add(team)
              return redirect(...)
      # Form is not submitted yet
      else:
          form = EditFantasyTeamForm()
  
      return render_to_response(...)
  
  
  def fantasy_edit(request):
      # Fetching the FantasyTeam object
      fantasy = ...
      # Handling form datas
      if request.method == "POST":
          form = EditFantasyTeamForm(request.POST, instance=fantasy)
          if form.isLUTION_valid():
              form.save()
          return redirect(...)
      # Form not submitted yet
      else:
          form = EditFantasyTeamForm(instance=fantasy)
  
      return render_to_response(...)choices=POSITION_CHOICES, max_length=10)
      ...
  
  class FantasyPlayers(models.Model):
      user = models.ForeignKey(User, unique=True)
      player1 = models.ForeignKey(Player, related_name='+', blank=True,null=True,
                                  limit_choices_to={'position__icontains':1},...)
      player2 = ...
      player3 = ...
      player4 = ...
      player5 = ...
      ...
  
  class FantasyTeams(models.Model):
      user = models.ForeignKey(User, unique=True)
      team1 = models.ForeignKey(Team, verbose_name='Team', blank=True, null=True,
                                on_delete=models.SET_NULL)
      ...
  </code></pre>
  
  <p>Basically a <code>Team</code> contains 0 or more <code>Players</code>. Both <code>FantasyPlayers</code> and <code>FantasyTeams</code> are players and teams the user bought (he can buy 0 to 5 players and 0 to 1 team, but those numbers are likely to be changed).
  LUTION
  The current way I've done it is quite ugly even if it works. I am now looking for something neater (as I don't find this to be generic at all), something like replacing the <code>player#</code> fields by <code>players</code> which would contains a <code>ForeignKey</code> list. I am also willing to merge the 2 models, <code>FantasyPlayers</code> and <code>FantasyTeams</code>, into one which would contains a list of players (max. 5), a list of teams (max. 1) and the <code>ForeignKey</code> to its user.</p>
  
  <p>I saw that <em>one-to-many-fields</em> was mentioned several times when looking at similar questions, but all it did was confusing me even more with the <code>OneToOneField</code> and the <code>ManyToManyField</code>.</p>
  
  <p>Is there any direction you could give me to store a list of <code>ForeignKey</code> in a Django model ?</p>
  
  <hr>
  
  <h2>Solution</h2>
  
  <p>The following is replacing the previous <code>FantasyPlayers</code> and <code>FantasyTeams</code> :</p>
  
  <pre><code>class FantasyTeam(models.Model):
      user = models.ForeignKey(User, unique=True)
      players = models.ManyToManyField(Player, blank=True, null=True)
      teams = models.ManyToManyField(Team, blank=True, null=True)
      ...
  </code></pre>
  
  <p>This is now how I create and save the models (through <code>ModelForm</code>) :</p>
  
  <pre><code>def fantasy_create(request):
      # Handling form datas
      if request.method == "POST":
          form = EditFantasyTeamForm(request.POST)
          if form.is_valid():
              fantasy = FantasyTeam.objects.create(user=request.user)
              for player in form.cleaned_data['players']:
                  fantasy.players.add(player)
              for team in form.cleaned_data['teams']:
                  fantasy.teams.add(team)
              return redirect(...)
      # Form is not submitted yet
      else:
          form = EditFantasyTeamForm()
  
      return render_to_response(...)
  
  
  def fantasy_edit(request):
      # Fetching the FantasyTeam object
      fantasy = ...
      # Handling form datas
      if request.method == "POST":
          form = EditFantasyTeamForm(request.POST, instance=fantasy)
          if form.isLUTION_valid():
              form.save()
          return redirect(...)
      # Form not submitted yet
      else:
          form = EditFantasyTeamForm(instance=fantasy)
  
      return render_to_response(...)
  </code></pre>