2

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...

2
  • Well - I wound up simplifying my model to get this to work so that there's only one model in the formset. But I'd still like to know how to do this... Commented Feb 1, 2011 at 17:59
  • did you ever find an answer for this? the link to yergler.net/blog/2009/09/27/nested-formsets-with-django doen't work in Django 1.2.5+ due to django ticket #11418 Commented Oct 4, 2011 at 7:34

3 Answers 3

2

I solved problem with http://yergler.net/blog/2009/09/27/nested-formsets-with-django/. Pleas use the following correction in forms.py file:

        instance=None
            pk_value = hash(form.prefix)

+       correct_data = None
+       if (self.data):
+           correct_data = self.data;
        # store the formset in the .nested property
        form.nested = [
-           TenantFormset(data=self.data,
+           TenantFormset(data=correct_data,
                            instance = instance,

Just working on Django 1.4.1 very well.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

TenantFormset = modelformset_factory(models.Tenant, extra=0)

form.nested = [
        TenantFormset(
                        queryset = Tenant.objects.filter(building = pk_value),
                        prefix = 'value_%s' % pk_value
                        )
                    ]

I also had to manually pass data to the sub-sub-forms in the is_valid method:

def is_valid(self):
result = super(BaseProtocolEventFormSet, self).is_valid()

for form in self.forms:
    if hasattr(form, 'nested'):
        for n in form.nested:
            n.data = form.data
            if form.is_bound:
                n.is_bound = True
            for nform in n:
                nform.data = form.data
                if form.is_bound:
                    nform.is_bound = True
            # make sure each nested formset is valid as well
            result = result and n.is_valid()
return result

EDIT:

New instances can be created using jQuery. See this question:

Comments

0

This sounds very similar to the approach talked about at help http://yergler.net/blog/2009/09/27/nested-formsets-with-django/ where Nathan writes about how he catered for "a multi-level data model; an example of this kind of model would be modeling City Blocks, where each Block has one or more Buildings, and each Building has one or more Tenants."

Some more explanations can aslo be found here Django Forms Newbie Question

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.