title: Saving Django formset with Many to Many Field
body: <p>I'm building a little app with a formset that lets me enter a number of game scores each linking to a player via a many to many field. I'm having problems getting this to save properly with various errors. Basic code.</p>
  
  <p>Model.py</p>
  
  <pre><code>class Player(models.Model):
      name = models.CharField(max_length=30)
  
      def __unicode__(self):
              return self.name
  
  class Score(models.Model):
      score = models.DecimalField(max_digits=4, decimal_places=0)
      turn = models.ForeignKey(Turn)
      players = models.ManyToManyField(Player)  
  
      def __unicode__(self):
              return self.score
  </code></pre>
  
  <p>Each score is then joined to a turn but I haven't shown that bit. </p>
  
  <p>Form.py</p>
  
  <pre><code>class NewScore(ModelForm):
  
      class Meta:
          model = Score
          fields = ('score', 'players',)
  </code></pre>
  
  <p>View.py</p>
  
  <pre><code>def newscore(request):
  
  NewScoreFormSet = formset_factory(NewScore, extra=2)  
  
  if request.method == 'POST':
  
      formset = NewScoreFormSet(request.POST)
  
      if formset.is_valid():
  
          t = Turn.objects.latest('id')
  
          for form in formset:
  
              p = form.cleaned_data.get('players')
  
              scoreSave = form.save(commit=False)
              scoreSave.turn = t
              scoreSave.save()
              scoreSave.players.add(p)
  
  
      else:
          print 'Formset not OK'
  
      return render(request, 'scorer/game.html',
                  {})
  
  else:
  
      formset = NewScoreFormSet()
  
      return render(request, 'scorer/game.html',
                  {'formset': formset,})
  </code></pre>
  
  <p>The issue seems to be around getting the player saved through the many to many field and in this current setup I get the error message </p>
  
  <blockquote>
    <p>TypeError: int() argument must be a string or a number, not 'QuerySet'</p>
  </blockquote>
  
  <p>I have also tried removing the line </p>
  
  <pre><code>scoreSave.players.add(p)
  </code></pre>
  
  <p>but that just doesn't save the player at all. Just the score and turn.</p>