Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Overriding fForm class __init__ method [RSS Feed]

#1 June 16, 2010 10:32:38

D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Overriding fForm class __init__ method


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 =
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

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 16, 2010 11:51:10

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Overriding fForm class __init__ method


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 =
> 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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 16, 2010 13:20:11

D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Overriding fForm class __init__ method


Thanks a lot. Your solution was the right one. However, I just needed
to provide 'pid' as the only argument for the pop method.

Now when I use the following command in my view it doesn't give me any
error:

form = VoteRadioForm(request.POST, pid = 2 )

However, I'm having a problem with form validation since is_valid
returns false. I think my form is not created correctly. Because when
I issue cleaned_data method, I get the following error:

'VoteRadioForm' object has no attribute 'cleaned_data'

I need to sleep now. Hopefully, tomorrow I am able to figure out this
problem.

Thanks

On Jun 16, 3:51 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> 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 =
> > 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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Overriding fForm class __init__ method [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support