title: Why do I need to save this model before adding it to another one?
body: <p>In django, I'm trying to do something like this:</p>
  
  <pre><code># if form is valid ...
  article = form.save(commit=False)
  article.author = req.user
  
  product_name = form.cleaned_data['product_name']
  try:
      article.product = Component.objects.get(name=product_name)
  except:
      article.product = Component(name=product_name)
  
  article.save()
  # do some more form processing ...
  </code></pre>
  
  <p>But then it tells me:</p>
  
  <blockquote>
    <p>null value in column "product_id" violates not-null constraint</p>
  </blockquote>
  
  <p>But I don't understand why this is a problem. When <code>article.save()</code> is called, it should be able the create the product <em>then</em> (and generate an id).</p>
  
  <p>I can get around this problem by using this code in the <code>except</code> block:</p>
  
  <pre><code>product = Component(name=product_name)
  product.save()
  article.product = product
  </code></pre>
  
  <p>But the reason this concerns me is because if <code>article.save()</code> fails, it will already have created a new component/product. I want them to succeed or fail together.</p>
  
  <p>Is there a nice way to get around this?</p>