Wednesday, August 8, 2012

formeset choicefield dynamic choices

Hi,

I'm working on a formset with dynamic forms. I've overloaded my form to be able to add new values in the select field but i cannot overload the formset constructor. Django raise exception : "__init__() takes exactly 1 argument (2 given)" - How can i change the way the formset_factory build each forms to be able to overload each form properly for the validation ?

forms.py:

class BaseColumnFormset(BaseFormSet):
my_choices = None

def __init__(self, **kwargs):
self.my_choices = kwargs['my_choices']
del kwargs['my_choices']
super(BaseColumnFormset, self).__init__(**kwargs)

def _construct_form(self, i, **kwargs):
kwargs["my_choices"] = self.my_choices
return super(BaseColumnFormset, self)._construct_form(i,**kwargs)

class ColumnForm(forms.Form):
column = forms.ChoiceField(choices = [('A', "lol")], widget=forms.Select, initial= 'A', label='Column')
preview = forms.CharField(widget=forms.Textarea)

def __init__(self, my_choices = None, *args, **kwargs):
super(ColumnForm, self).__init__(*args, **kwargs)
self.fields['column'].choices = my_choices

view.py:

formset_column = formset_factory(ColumnForm, formset = BaseColumnFormset)
column_formset = formset_column(request.POST)

Django raise : "__init__() takes exactly 1 argument (2 given)"

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/-yYKMVO187QJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate