Say I have django model that looks something like this:
class Order(models.Model):
number = models...
date = models...
class OrderLine(models.Model):
# One or more lines per order
order = models.ForeginKey(Order)
common_line = models.OneToOneField(CommonLine)
class CommonLine(models.Model):
# common elements of what might be on a line item...
taxes = model...
amount = model...
I want to create a form that uses an inlineformset to edit one or more Lines (both OrderLine and CommonLine) per order.
I can create a formset that works with Order and OrderLine – but how do I get the inline formset to give me all the detailed items from the CommonLine class when displaying the formset. It seems the documentation on inline formsets requires that the inline form – the multiple lines on an order can only map to a single class…
Am I not seeing something in the documentation? I’m sure I can probably override something, I’m just not sure where.
Thanks for any help…
Some minor changes were needed to make Nathan’s code at http://yergler.net/blog/2009/09/27/nested-formsets-with-django/ work in Django 1.3. The line below causes a ManagementForm Error.
TenantFormset = inlineformset_factory(models.Building, models.Tenant, extra=1)Usings the modelformset_factory and manually defining the queryset seems to work, but I have not implemented the ability to add extras.
I also had to manually pass data to the sub-sub-forms in the is_valid method:
EDIT:
New instances can be created using jQuery. See this question: