How would I create a form for a foreign key field that has a drop down menu with an 'add item' option in django?
<p>I'll start with my model fields:  </p>

<pre><code>class Store(models.Model):
    name = models.CharField(max_length=250)

    def __str__(self):
        return self.name


class Product(models.Model):
    type = models.CharField(max_length=250)

    def __str__(self):
        return self.type


class Receipt(models.Model):
    store = models.ForeignKey(Store)
    date = models.DateField()
    line_items = models.ManyToManyField(Product, through='ReceiptProduct')

    def __str__(self):
        return self.store.name + ': ' + str(self.date)


class ReceiptProduct(models.Model):
    receipt = models.ForeignKey(Receipt)
    product = models.ForeignKey(Product)
    price = models.FloatField()
    description = models.CharField(max_length=500, null=True, blank=True)

    def __str__(self):
        return self.product.type  
</code></pre>

<p>What I would like to do is create a form for the ReceiptProduct model. </p>

<pre><code>class AddItemForm(ModelForm):
    class Meta:
        model = ReceiptProduct
        fields = ['product', 'price', 'description']
</code></pre>

<p>Done. And the view?  </p>

<pre><code>def add_receipt_product(request, receipt_id):
    current_receipt = Receipt.objects.get(id=receipt_id)
    if request.method != 'POST':
        # No data submitted; create a blank form.
        form = AddItemForm(initial={'receipt': current_receipt})
    else:
        # POST data submitted; process data.
        form = AddItemForm(data=request.POST)
        if form.is_valid():
            new_product = form.save(commit=False)
            new_product.receipt = current_receipt
            new_product.save()
            return HttpResponseRedirect(reverse('purchase_log:receipt_details', args=[receipt_id]))

    context = {'current_receipt': current_receipt, 'form': form}
    return render(request, 'purchase_log/add_receipt_product_form.html', context)  
</code></pre>

<p>Okay, so what I would like to do is, under the 'product' field (which is a drop down menu populated by the Product model), have an option called, maybe, 'custom product' or something, that the user can select to add an item to the Product model and will then appear in future drop down menus. Is this do-able?<br>
Thank you all in advanced!!</p>

<p>Django implements this in terms of <a href="https://docs.djangoproject.com/en/1.10/topics/forms/formsets/" rel="nofollow">a "formset"</a>. Check out this tutorial for additional information: <a href="http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html" rel="nofollow">http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html</a> I think the example there is fairly similar to yours.</p>

<p>In the Django admin interface, things are somewhat easier, and you can use an <a href="https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.TabularInline" rel="nofollow"><code>Inline</code></a>. </p>

python django django-forms foreign-keys modelform