django inline formsets with a complex model for the nested form

ぃ、小莉子 提交于 2019-12-18 05:25:08

问题


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


回答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:




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




回答3:


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



来源:https://stackoverflow.com/questions/4832672/django-inline-formsets-with-a-complex-model-for-the-nested-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!