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:
This is the original form:
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)
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)
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)
I get this Error:
File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py" in inner 39. response = get_response(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)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
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)
File "/Library/Python/2.7/site-packages/django/forms/formsets.py" in formset_factory 449. return type(form. name + str('FormSet'), (formset,), attrs)
Exception Type: AttributeError at /showroom/po_populate/3/ Exception Value: 'PO_Form' object has no attribute ' name '
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, }
])
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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.