title: save() prohibited to prevent data loss due to unsaved related object: How to save related models together?
body: <p>I have a form and a formset. Formset contains a column foreign key to form. However my forms are not saving and <code>save() prohibited to prevent data loss due to unsaved related object</code> error is being thrown. How could I save both these data together?</p>
  
  <p>here's my view:</p>
  
  <pre><code>def purchaseOrderView(request):
      if request.method == 'POST':
          formset = POFormSet(request.POST)
          form = POHeaderForm(request.POST)
          print("POSTED")
          if form.is_valid() and formset.is_valid():
              form.save()
              formset.save()
              messages.success(request,"VALID SUBMISSION")
              return render(request,'purchase_order.html',{'formset':formset, 'form':form})
          else:
              return render(request, 'purchase_order.html', {'formset': formset, 'form': form})
      else:
          formset = POFormSet()
          form = POHeaderForm()
          return render(request,'purchase_order.html',{'formset':formset, 'form':form})
  </code></pre>
  
  <p>here's my model structure:</p>
  
  <pre><code>class POHeaderModel(models.Model):
      date = models.DateTimeField(blank=False, default=timezone.now)
      reference = models.CharField(validators=[alphanumeric], max_length=25, blank=True, null=True)
      supplier = models.ForeignKey(SuppliersModel, on_delete=models.PROTECT)
      note = models.CharField(validators=[alphanumeric], max_length=300, blank=True, null=True)
      total = models.DecimalField(decimal_places=2, max_digits=10, validators=[MinValueValidator(0)])
  
  class POBodyModel(models.Model):
      PO = models.ForeignKey(POHeaderModel, on_delete=models.PROTECT)
      item_number = models.CharField(validators=[alphanumeric], max_length=25, blank=True, null=True)
      description = models.CharField(validators=[alphanumeric], max_length=100, blank=True, null=True)
      quantity = models.IntegerField(blank=True, null=True)
      rate = models.DecimalField(decimal_places=2, max_digits=10)
      discount = models.DecimalField(decimal_places=2, max_digits=10,blank=True, null=True)
      total = models.DecimalField(decimal_places=2, max_digits=10, blank=False)
  </code></pre>
  
  <p>There are a lot of examples out there in various blogs and in here in questions using <code>with transaction.atomic():</code>. But none works or may be doesn't suit my scenario. Any advise?</p>