forms.ChoiceField and empty values

1,056 views
Skip to first unread message

Monika Sulik

unread,
Nov 10, 2009, 9:03:52 AM11/10/09
to Django users
Hi,

I have the following problem... I've got a form, which looks like
this:

class CompetitionSearchForm(forms.Form):
name = forms.CharField(required=False)
type = forms.ChoiceField(choices=COMPETITION_TYPE_CHOICES,
required=False)

The tuple COMPETITION_TYPE_CHOICES is used in a model class (the
purpose of the form is to search through Competition objects). It
looks something a bit like this:

COMPETITION_TYPE_CHOICES = (
(1, 'Olympic Games'),
(2, 'ISU Championships'),
(3, 'Grand Prix Series'),
)

I'd like the select widget in ChoiceField to display an empty label.
According to the documentation (http://docs.djangoproject.com/en/dev/
ref/forms/fields/#choicefield), it seems like this should happen by
default, but I don't get an empty value in the select widget at all.
Why is this?

Monika

Monika Sulik

unread,
Nov 12, 2009, 9:40:18 AM11/12/09
to Django users
As I haven't had any replies so far, I'll add some more information...

The empty label appears if I redefine the form like this:

class CompetitionSearchForm(forms.ModelForm):
class Meta:
model = Competition
fields = ('name','type')

It disappears again if I make the changes I need to on the fields
(i.e. "unrequire" them):

class CompetitionSearchForm(forms.ModelForm):
name = forms.CharField(required=False)
type = forms.ChoiceField(choices=COMPETITION_TYPE_CHOICES,
required=False)
class Meta:
model = Competition
fields = ('name','type')

Should I be defining my type field somehow differently? Or is this a
bug in Django that I should report?

Monika Sulik

unread,
Nov 12, 2009, 9:40:29 AM11/12/09
to Django users
As I haven't had any replies so far, I'll add some more information...

The empty label appears if I redefine the form like this:

class CompetitionSearchForm(forms.ModelForm):
class Meta:
model = Competition
fields = ('name','type')

It disappears again if I make the changes I need to on the fields
(i.e. "unrequire" them):

class CompetitionSearchForm(forms.ModelForm):
name = forms.CharField(required=False)
type = forms.ChoiceField(choices=COMPETITION_TYPE_CHOICES,
required=False)

rebus_

unread,
Nov 12, 2009, 9:55:13 AM11/12/09
to django...@googlegroups.com
2009/11/12 Monika Sulik <monik...@e.pl>:
What does model Competition look like?
Do you have choices defined in your models.py and use choices argument
when creating that field in models?

Monika Sulik

unread,
Nov 13, 2009, 6:29:08 AM11/13/09
to Django users
Sorry for the double post *blush* Not sure how that happened...

The model looks something like this:

class Competition(MessageboardOwner):
name = models.CharField(max_length=256)
type = models.IntegerField(choices=COMPETITION_TYPE_CHOICES)
start = models.DateField()
end = models.DateField()
official_www = models.URLField(blank=True)
Reply all
Reply to author
Forward
0 new messages