title: How to save Django unsaved model with one-to-many relationship
body: <p>I have a Django 1.5 one to many relationship between 2 models. It looks something like this</p>
  
  <pre><code>class Team(models.Model):
      class Meta:
      league = models.ForeignKey(League,related_name="teams")
      ...
  
  class League(models.Model):
      ...
  </code></pre>
  
  <p>Each League can have one or more teams. I am responding to a POST request on a League related URL and in the process of creating (or updating) a League. Teams can only be created indirectly through Leagues (ie. no POST of Team is supported) and each Team must have a League. However I cannot get it to work correctly. </p>
  
  <p>In the <code>LeagueSerializer</code> I have overridden <code>from_native</code> and parsing json there (I do not have a <code>TeamSerializer</code>). When there is validation error from <code>full_clean()</code> I return None (from <code>from_native()</code>). In my view, after calling <code>LeagueSerializer.is_valid()</code> I check if <code>LeagueSerializer.object</code> is not <code>None</code>, and if it is not - I call <code>LeagueSerializer.save()</code>. And this seems to work great when I am not changing the teams in the League. Here is the snippet from <code>views.py</code> (as requested by TankorSmash)</p>
  
  <pre><code>def league_submit(request):
      if request.method == 'POST':
          #pass the incoming post data to league serializer
          serializer = LeagueSerializer(data=request.body)
          if serializer.is_valid():
              if serializer.object is None:
                  print "something went awry in from_native. invalid/bad data in post"
              else:
                  print "things seemed to have been ok in from_native"
                  serializer.save()
  </code></pre>
  
  <p>Any attempt to add a team to unsaved league instance by calling league.teams.add(new_team) fails with Integrity error</p>
  
  <pre><code>IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails 
  </code></pre>
  
  <p>How can I save the unsaved <code>League</code> with one to many foreign key relationship ? I would like to keep the current processing flow.</p>
  
  <p>EDIT -
  I have since found a workaround to this. </p>
  
  <p>1.I had to associate a League object from DB temporarily just so I can call new_team.full_clean(). Once that is done, I set new_team.league = new_league. This is so I can validate the new_team that will be eventually associated with the new_league that is not in DB yet.</p>
  
  <p>2.In order to pass the new_team to my view, I introduced an unsaved_objects non-model list into League model. The view calls save() on the League object and then iterates unsaved_objects and calls save on each of those objects.</p>
  
  <p>Here is what I have learnt so far about this - Django 1.5 does not provide a way for you to create a new League, a new team and associate the team with the league and call save once on any one object (be it serializer or a model) to save the League, its relationship to the team and the team. Django needs save on League followed by save on Team.</p>
  
  <p>I call the above a workaround because it does not serve my purpose entirely. For example if team save fails, DB would have processed the League save already thus requiring me to track (a) when new_league is created so i can delete the new league (b) previous league contents when updating an existing league so i can revert to the previous league. If I could have a transaction across the save() calls, that would take care of this. </p>
  
  <p>Querying db constantly for the temporary league is a waste.</p>