Problme in creating dynamic ChoiceField

28 views
Skip to first unread message

VIPUL BANSAL

unread,
Aug 5, 2014, 8:28:28 AM8/5/14
to django...@googlegroups.com
Hi,

I trying to create a drop-down which gets repopulated every time we land on the page.

If I use the following code everything works correctly:

class DeleteMappingForm(forms.Form) :
    subAreaDropDown = forms.ChoiceField(choices = fetchChoices())

def deleteMapping(request):
        form = DeleteMappingForm()
        return render(request,'polls/deleteMapping.html', {'form' : form })


But when I try to make things dynamic, things do not run

class DeleteMappingForm(forms.Form) :
    def _init_(self, *args, **kwargs):
        super(DeleteMappingForm, self)._init_(*args, **kwargs)
        self.fields['subAreaDropDown'] = forms.ChoiceField(choices = fetchChoices())

def deleteMapping(request):
        form = DeleteMappingForm()
        return render(request,'polls/deleteMapping.html', {'form' : form })

P.S. : fetchChoices() is a function which returns me the choices.

Can some one help me identify the problem?

Thanks in advance.

Daniel Roseman

unread,
Aug 5, 2014, 9:38:34 AM8/5/14
to django...@googlegroups.com
The initialization method is called __init__ (two underscores each side), not _init_.
--
DR. 

VIPUL BANSAL

unread,
Aug 5, 2014, 12:09:18 PM8/5/14
to django...@googlegroups.com
Hi Daniel,

It worked.
Thanks a lot :)
Reply all
Reply to author
Forward
0 new messages