views:

402

answers:

3

What I'm trying to do

I'm going to be keeping data about competitions in my database. I want to be able to search the competitions by certain criteria - competition type in particular.

About competition types

Competition types are kept in a tuple. A slightly shortened example:

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

These are used in the model like so (again - this is a shortened/simplified version of the model):

class Competition(models.Model):
    name = models.CharField(max_length=256)
    type = models.IntegerField(choices=COMPETITION_TYPE_CHOICES)

The search form

I don't want the fields to be required in the search form, so the form is defined like this:

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

The problem

I'd like the select widget in ChoiceField to display an empty label, but I don't get one. Any help with this would be much appreciated :)

A: 

Try adding blank=True to the model fields (assuming that's the behavior you want), then changing the form to a ModelForm and removing the field definitions. Note that any fields for which you set blank=True won't be required when validating or saving the model. Again, this may not be what you want but if it is it'll allow Django to take care of a few things automatically.

Otherwise just change your COMPETITION_TYPE_CHOICES to:

COMPETITION_TYPE_CHOICES = (
    ('', '---------'),
    ('1', 'Olympic Games'),
    ('2', 'ISU Championships'),
    ('3', 'Grand Prix Series'),
)
John Debs
Thanks, this answer is useful (it didn't occur to me that you could define the value as '' - so I've learnt something :)). But unfortunately it's not the answer I'm looking for either...blank=True is definitely not what I want and changing the tuple like you suggest has a couple of drawbacks:a) It means I have two '---------' options in the admin nowb) If, for example, I decide I want the empty label in the search form, but don't want it in an add competition form then I'm screwed basically.
Monika Sulik
+1  A: 

I've found a solution that works the way I want it to without violating the DRY principle. Not very clean, but it'll have to do I suppose.

According to the documentation choices don't have to be a tuple:

Finally, note that choices can be any iterable object -- not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices to be dynamic, you're probably better off using a proper database table with a ForeignKey. choices is meant for static data that doesn't change much, if ever.

So the solution I'm going with for the moment is:

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

COMP_TYPE_CHOICES_AND_EMPTY = [('','All')] + COMPETITION_TYPE_CHOICES

And then:

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

The model stays the same as it was.

Monika Sulik
A: 

Better choice is to update field choices in form init method

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


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

    def __init__(self, *args, **kwargs):
        super(CompetitionSearchForm, self).__init__(*args, **kwargs)
        self.fields['type'].choices.insert(0, ('','---------' ) )
Evgeniy