How to work with Django inline forms?
<p>I have two models (Receipt and Ingredient) and want to create ingredients when creating receipt.</p>

<p><strong>forms.py</strong></p>

<pre><code>from django import forms
from django.forms import ModelForm
from django.forms.models import inlineformset_factory
from .models import Receipt, Ingredient, CookingStep


class ReceiptForm(forms.ModelForm):

  class Meta:
    model = Receipt
    fields = ('name', 'description', 'hour', 'minute', 'multivarka',)

ReceiptFormSet = inlineformset_factory(Receipt, Ingredient, fields=('name', 'count', 'value'))    
</code></pre>

<p><strong>models.py</strong></p>

<pre><code>from django.db import models

class Receipt(models.Model):
  name = models.CharField(max_length=255)
  description = models.TextField(max_length=1024)
  hour = models.IntegerField(default=0)
  minute = models.IntegerField(default=1)
  multivarka = models.BooleanField(default=False)

  def __str__(self):
    return "%s" % (self.name)

class Ingredient(models.Model):
  name = models.CharField(max_length=255)
  count = models.IntegerField(default=0) #200gr, 1kg etc
  value = models.CharField(max_length=255) #gramm, kg etc
  receipts = models.ForeignKey(Receipt)

  def __str__(self):
    return "%s" % (self.name)
</code></pre>

<p><strong>views.py</strong></p>

<pre><code>def receipt_new(request):
    if request.method == "POST":
        formset = ReceiptForm(request.POST)
        if formset.is_valid():
            created_receipt = formset.save(commit=False)
            new_formset = ReceiptFormSet(request.POST, instance=created_receipt)
            if new_formset.is_valid():
              created_receipt.save()
              new_formset.save()
              return redirect('/receipts')
    else:
        formset = ReceiptForm()
        inline_form = ReceiptFormSet()
    return render(request, 'admin_site/receipt_edit.html', {'formset': formset})
</code></pre>

<p><strong>receipt_edit.html</strong></p>

<pre><code>{% block content %}
    &lt;h1&gt;New receipt&lt;/h1&gt;
    &lt;form method="POST" class="post-form"&gt;{% csrf_token %}
        {{ formset.as_p }}
        &lt;button type="submit" class="save btn btn-default"&gt;Save&lt;/button&gt;
    &lt;/form&gt;
{% endblock %}
</code></pre>

<p>Now only form for creating receipt is displayed. I want to have two forms on edit page: firstly form for receipt and then form for creating ingredient for this receipt (on one page).</p>

<p>Firstly, use some consistent names for your variables. Your <code>ReceiptForm</code> instance is a form, not a formset, so call it <code>form</code>. Let's call your <code>ReceiptFormSet</code> instance <code>formset</code> everywhere.</p>

<p>Next, you need to include your form and formset in the template context when you call <code>render</code>.</p>

<pre><code>def receipt_new(request):
    if request.method == "POST":
        form = ReceiptForm(request.POST)
        if form.is_valid():
            created_receipt = form.save(commit=False)
            formset = ReceiptFormSet(request.POST, instance=created_receipt)
            if formset.is_valid():
              created_receipt.save()
              formset.save()
              return redirect('/receipts')
    else:
        form = ReceiptForm()
        formset = ReceiptFormSet(instance=Receipt())

    return render(request, 'admin_site/receipt_edit.html', {'form': form, 'formset': formset})
</code></pre>

<p>Finally, you need to include your form and formset in the template.</p>

<pre><code>&lt;form method="POST" class="post-form"&gt;{% csrf_token %}
    {{ form }}
    {{ formset }}
    &lt;button type="submit" class="save btn btn-default"&gt;Save&lt;/button&gt;
&lt;/form&gt;
</code></pre>

python django