On Jun 16, 10:30 am, DevelopsDjangoApps <mom...@gmail.com> wrote:
> Hi,
>
> I have created a form class like this:
>
> class VoteRadioForm(forms.Form):
>
>     choices =
> forms.ModelChoiceField(queryset=Choice.objects.filter(poll__active=
> True, poll__id = 2),
>                                empty_label= None,
>                                widget=  forms.RadioSelect,
>     )
>
> This class gives me dynamic number of entries for my form, based on
> the poll_id. But as you may have already seen it, the poll__id is
> hardcoded in this example. In order to pass different values to
> poll__id, I tried to generated my choices element inside the __init__
> method like this:
>
> #choices =
> forms.ModelChoiceField(queryset=Choice.objects.filter(poll__active=
> True, poll__id = 2),
>     def __init__(self, pid = None, *args, **kwargs):
>         super(VoteRadioForm, self).__init__()
>         self.fields['ch'] =
> forms.ModelChoiceField(queryset=Choice.objects.filter(poll__active=
> True, poll__id = pid),
>                                     empty_label= None,
>                                     widget=  forms.RadioSelect,
>         )
>
> The first example(Hardcoded version) works perfectly fine. The second
> example generates the form correctly, when I use:
>
> form = VoteRadioForm(i)
>
> However, when I try to validate the data and pass the request object
> to it, it gives me the following error:
>
> int() argument must be a string or a number, not 'QueryDict'
>
> I even tried to add *args and **kwargs arguments when initializing my
> class, just like this example:
>
> http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/
>
> But, it doesn't make any difference.
>
> I'm having Pythong 2.6.5 on my Arch Linux machine.
>
> I was wondering if anyone has ever had such problem?
>
> Thanks in advance

It's because your 'pid' parameter is grabbing the first argument to
the function, which is usually the posted data.

Instead, do this:

    def __init__(self, *args, **kwargs):
        pid = kwargs.pop('pid', None)
        super(VoteRadioForm, self).__init__(*args, **kwargs)
        ...etc...

--
DR.

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

Reply via email to