You could perhaps subclass ChoiceField to allow for custom empty_labels. This code is highly untested and written so casually it shouldn't be pasted anywhere.
# your forms.py from django.forms.fields import ChoiceField class MyCustomChoiceField(ChoiceField): def __init__(self, *args, **kwargs): self.empty_label = kwargs.pop('empty_label', None) super(MyCustomChoiceField, self).__init__(*args, **kwargs) self.choices = self.custom_choices(choices) def custom_choices(self, choices): if self.empty_label is not None: yield (u"", self.empty_label) for choice in self.choices: yield choice # ---- I assume something like this would work? I actually believe it should even come with django. Cheers, AT On Thu, Dec 22, 2011 at 11:05 AM, Tom Evans <tevans...@googlemail.com>wrote: > On Thu, Dec 22, 2011 at 12:37 PM, talbronstien <talbronst...@gmail.com> > wrote: > > Hi, > > > > I want to have a field in a form class which will render as select in > > html. But, I want default text shown as "Select Blood Type". My > > declaration is > > > > gender = forms.ChoiceField(required=False,choices = > > USER_BLOOD_CHOICES). > > > > But, if I add an option value in html, as <option value="0">Select > > Blood Type"</option>. I get an error, as this is not valid value (as > > this is not required and user may not select an option). How can I > > handle this? > > > > thanks > > > > Don't add an option in HTML. Job done. > > The way I normally do this is to add the invalid choice into the list > of choices, and then assert that their choice is not the invalid > choice: > > class FooForm(forms.Form): > INVALID_CHOICE = -1 > WIBBLE = 1 > FOO_CHOICES = ( > (INVALID_CHOICE, "Select..."), > (WIBBLE, 'Wibble'), ) > fooselect = forms.ChoiceField(choices=FOO_CHOICES) > > def clean_fooselect(self): > if self.cleaned_data.get('foochoice') == INVALID_CHOICE: > raise forms.ValidationError('Your choice sucked') > > Cheers > > Tom > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@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. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.