passing information from a form instance to a field in that form

1 view
Skip to first unread message

Jim

unread,
Sep 22, 2009, 3:59:25 PM9/22/09
to Django users
Hello,

I have a widget that makes suckerfish menus (the kind of menu that is
written in css and where if you hover your mouse over a menu item any
subitems appear).

I have some data that can be displayed according to three dimensions.
(For instance, I could characterize contributers by gender, by
political persuasion, and by nationality.) I want to get the tree of
choices out of the database and to the widget, by dimension.

Thus, if the url is contributer/gender then the choices might be
[ ('male', ['male'] ), ('female', ['female']) ]
while if the url is contributer/politics then the choices given to the
widget might be this.
[ ('democrat', ['democrat']), ('republican', ['republican']),
('looney', ['looney']),
('looney > right wing', ['looney','right wing']), ('looney >
left wing', ['looney','left wing'])]
(I could show the widget if folks thought it would be helpful.)

I have this form and field.

class CharacterizationForm(forms.Form):
def __init__(self,*args,**keywords):
if dimen in keywords: # make any sense?
self.dimen=dimen
super(CharacterizationForm,self).__init__(*args,**keywords)

pick=CharacterizationField
(required=True,widget=widgets.CharacterizationWidget)

class CharacterizationField(forms.ChoiceField):
--some stuff--
super(CharacterizationField, self).__init__(choices=choices,
required=required, widget=widget, label=label, initial=initial)
self.widget.choices=self.choices

Of course, in the view I initialize the form in some way.
f=MenuForm(initial={'dimen',dimen})
came to mind, although perhaps that is not right? Now, how do I get
the information about the dimen into the "--some stuff--", where I can
then set up the choices from it?

I have been looking at the forms and fields, but I admit I'm not
getting it. Any help greatly appreciated.

Jim

Daniel Roseman

unread,
Sep 22, 2009, 5:12:37 PM9/22/09
to Django users
Do it all in the form's __init__.

class CharacterizationForm(forms.Form):
def __init__(self, *args, **kwargs):
dimen = kwargs.pop('dimen', None)
super(CharacterizationForm,self).__init__(*args, **kwargs)
if dimen:
self.fields['pick'].dimen = dimen

--
DR.
Reply all
Reply to author
Forward
0 new messages