On Aug 2, 11:06 pm, Jim <jim.heffe...@gmail.com> wrote: > Hello, > > I have a custom field whose behavior depends on a parameter show_all. > (It makes the field pull from the database for display only those > keywords where show=True.) > > class KeywordsField(forms.ChoiceField): > def __init__(self, choices=(), required=True, > widget=widgets.KeywordsWidget, label=None, initial=None, > show_all=False, help_text=None): > self.show_all=show_all > super(KeywordsField, self).__init__(choices=self.choices, > required=required, widget=widget, label=label, initial=initial, > help_text=help_text) > > (edited; I hope I didn't introduce any typos.) > > I have a form that I'd like to call as > f0=KeywordsForm(show_all=True) > and pass show_all to the field. Here is my try. > > class KeywordsForm(forms.Form): > keywords=KeywordsField(required=False) > > def __init__(self, show_all=False, *args, **kwargs): > self.show_all = show_all > super(KeywordsForm, self).__init__(*args, **kwargs) > self.fields['keywords'].show_all=show_all > > Setting self.fields didn't work (or I wouldn't be writing). Neither > did calling this. > keywords=KeywordsField(required=False,show_all=self.show_all) > Neither did a fair amount of looking at the documentation, the source, > and googling, although no doubt I didn't do it right. > > Can someone give me a hint about how I should do it? I'd be grateful. > Jim
This code looks absolutely right. What do you mean by 'setting self.fields didn't work'? How did it not work? What didn't happen that you expected to happen? Did you get errors? That is the usual way of dynamically setting an attribute on a field, and I've used it many times with no problems. -- 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.