带有嵌套表单的复杂模型的 django 内联表单集 [英] django inline formsets with a complex model for the nested form

查看:25
本文介绍了带有嵌套表单的复杂模型的 django 内联表单集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有看起来像这样的 django 模型:

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

我想创建一个表单,该表单使用内联表单集来编辑每个订单的一个或多个行(OrderLine 和 CommonLine).

I want to create a form that uses an inlineformset to edit one or more Lines (both OrderLine and CommonLine) per order.

我可以创建一个适用于 Order 和 OrderLine 的表单集 - 但是我如何获取内联表单集以在显示表单集时为我提供来自 CommonLine 类的所有详细项目.似乎内联表单集的文档要求内联表单 - 订单上的多行只能映射到单个类...

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.

感谢您的帮助...

推荐答案

http://yergler.net/blog/2009/09/27/nested-formsets-with-django/ 在 Django 1.3 中工作.下面的行会导致 ManagementForm 错误.

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)

使用 modelformset_factory 并手动定义查询集似乎有效,但我还没有实现添加额外内容的能力.

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
                        )
                    ]

我还必须在 is_valid 方法中手动将数据传递给子表单:

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

可以使用 jQuery 创建新实例.请参阅这个问题:

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

这篇关于带有嵌套表单的复杂模型的 django 内联表单集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆