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

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

问题描述

说我有django模型看起来像这样:

  class Order(models.Model):
number = models ...
date = models ...

class OrderLine(models.Model):
#每个订单一行或多行
order = models .ForeginKey(Order)
common_line = models.OneToOneField(CommonLine)

class CommonLine(models.Model):
#可能在一个行项目上的常见元素
taxes = model ...
amount = model ...

我想创建一个窗体,使用一个inlineformset来编辑一个或多个Line(每个OrderLine和CommonLine)每个订单。



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



我没有看到某些内容文件?我确定我可以覆盖一些东西,我只是不知道在哪里。



感谢任何帮助...

解决方案

需要进行一些细微的修改才能使Nathan的代码在 http://yergler.net/blog/2009/09/27/nested-formsets-with-django/ 在Django 1.3中工作。以下行导致ManagementForm错误。



TenantFormset = inlineformset_factory(models.Building,models.Tenant,extra = 1)



使用modelformset_factory并手动定义查询器似乎有效,但我没有实现添加附加功能。

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

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

]

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

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

for self.forms中的表单:
如果hasattr (form,'nested'):
在form.nested中的n:
n.data = form.data
如果form.is_bound:
n.is_bound = True
n中的nform:
nform.data = form.data
if form.is_bound:
nform.is_bound = True
#确保每个嵌套表单都有效
result = result和n.is_valid()
返回结果

编辑:



可以使用jQuery创建新的实例。请参阅此问题


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.

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:

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

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