Re: passing a keyword argument from form to a field in that form
On Aug 3, 3:59 am, Daniel Rosemanwrote: > This code looks absolutely right. Boy there's a phrase I don't hear often! > 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? If I put in a statement logging the value of self.show_all, it is unaffected by whether I call KeywordsForm as KeywordsForm(show_all=True) or KeywordsForm(show_all=False). > That is the usual way of dynamically setting an attribute on a field, > and I've used it many times with no problems. Thank you, Daniel; it is helpful to know that I have the right approach but must have a bug somewhere. I'll have another whack at it-- sometimes a nights sleep makes plain some imbecility on my part. :-) Jim -- 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.
Re: passing a keyword argument from form to a field in that form
On Aug 2, 11:06 pm, Jimwrote: > 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.
passing a keyword argument from form to a field in that form
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 -- 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.