Formset初始选择字段 [英] Formset initial choice field

查看:49
本文介绍了Formset初始选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要传递给Formset的表单(正常工作),但是传递给ChoiceFields的元组Im未被呈现或出现错误:

I have a form (working correctly) which I want to pass to a Formset, but the tuples Im passing for the ChoiceFields are not rendered or have a error:

这是原始格式:

class PO_Form(forms.Form):
    def __init__(self, baseItem_choices, color_choices, material_choices, sizeGroup_choices, *args, **kwargs):
        super(PO_Form, self).__init__(*args, **kwargs)
        self.fields['base_item'].choices = baseItem_choices
        self.fields['color_or_print'].choices = color_choices
        self.fields['material'].choices = material_choices
        self.fields['size_group'].choices = sizeGroup_choices

    base_item = forms.ChoiceField(choices=(), required=True)
    color_or_print = forms.ChoiceField(choices=(), required=True)
    material = forms.ChoiceField(choices=(), required=True)
    size_group = forms.ChoiceField(choices=(), required=True)

此表单ChoiceFields是由我在视图中创建的各种touples列表填充的:

this form ChoiceFields are populated from various lists of touples which I create in a view:

form = PO_Form(baseItem_choices, color_choices, material_choices, sizeGroup_choices)

我如何在表单集中进行这项工作?我尝试了两种方法:1:

How I make this work in a formset? I tried two approaches: 1:

PO_FormSet = formset_factory(PO_Form(baseItem_choices, color_choices, material_choices, sizeGroup_choices), extra=2)

我收到此错误:

文件"/Library/Python/2.7/site-packages/django/core/handlers/exception.py"在内部39. response = get_response(request)

File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py" in inner 39. response = get_response(request)

文件"/Library/Python/2.7/site-packages/django/core/handlers/base.py"在_get_response中187. response = self.process_exception_by_middleware(e,request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)

文件"/Library/Python/2.7/site-packages/django/core/handlers/base.py"在_get_response中185. response = wrapd_callback(request,* callback_args,** callback_kwargs)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

文件"/Users/carlospceballos/Dropbox(个人)/django/projectos/WholeSale/ShowRoom/views.py"中PO_formset_populate115. PO_FormSet = formset_factory(PO_Form(baseItem_choices,color_choices,material_choices,sizeGroup_choices),extra = 2)

File "/Users/carlospceballos/Dropbox (Personal)/django/projectos/WholeSale/ShowRoom/views.py" in PO_formset_populate 115. PO_FormSet = formset_factory(PO_Form(baseItem_choices, color_choices, material_choices, sizeGroup_choices), extra=2)

文件"/Library/Python/2.7/site-packages/django/forms/formsets.py"位于formset_factory449. return type(form. name + str('FormSet'),(formset,),attrs)

File "/Library/Python/2.7/site-packages/django/forms/formsets.py" in formset_factory 449. return type(form.name + str('FormSet'), (formset,), attrs)

异常类型:/showroom/po_populate/3/处的AttributeError值:'PO_Form'对象没有属性'名称'

Exception Type: AttributeError at /showroom/po_populate/3/ Exception Value: 'PO_Form' object has no attribute 'name'

2:我尝试在视图中设置初始值:

2: I tried setting the initial values in the view:

PO_FormSet = formset_factory(PO_Form(), extra=2)
formset = PO_FormSet(initial=[
    {   'base_item': baseItem_choices,
        'color_or_print': color_choices,
        'material': material_choices,
        'size_group': sizeGroup_choices, }
])

如果我不修改PO_Form,则会出现错误,表示该表单接受5个参数,而Im仅传递1.如果我修改了表单(去除了 init ),我不会出错,但是选择字段为空...我在做什么错了?

If I don't modified PO_Form an error arises saying the the form takes 5 arguments and Im only passing 1. If I modify the form (strip away the init) I get no error but the Choice Fields are empty... What Im doing wrong?

推荐答案

您正在将表单实例而不是表单类传递给 formset_factory 方法.您可以在实例化表单集之后设置选择.

You are passing the instance of the form instead of the form class to the formset_factory method. You can set the choices after instantiating the formset.

forms.py:

class PO_Form(forms.Form):
    base_item = forms.ChoiceField(choices=(), required=True)
    color_or_print = forms.ChoiceField(choices=(), required=True)
    material = forms.ChoiceField(choices=(), required=True)
    size_group = forms.ChoiceField(choices=(), required=True)

views.py:

PO_FormSet = formset_factory(PO_Form)

formset = PO_FormSet()

for form in formset.forms:
    form.fields['base_item'].choices = baseItem_choices
    form.fields['color_or_print'].choices = color_choices
    form.fields['material'].choices = material_choices
    form.fields['size_group'].choices = sizeGroup_choices

这篇关于Formset初始选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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