views:

18

answers:

2

If a model's field is a choice or foreign key, the widget on the page is a select input or radios if you specify that. Django places "---------" in the first item as an unselected value. How can I override it or even remove it? I remember reading the way to do it but can't find it any more.

+2  A: 

See : http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

If the model field has choices set, then the form field's widget will be set to Select, with choices coming from the model field's choices. The choices will normally include the blank choice which is selected by default. If the field is required, this forces the user to make a selection. The blank choice will not be included if the model field has blank=False and an explicit default value (the default value will be initially selected instead).

Piotr Duda
A: 
class TestForm(ModelForm): 
    class Meta:
        model = Test

    def __init__(self, *args, **kwargs):
        super(self.__class__, self).__init__(*args, **kwargs)
        self.fields['baz'].choices = [(foo.id, foo.description) for foo in Foo.objects.all()]

Test to see if this works.

Seitaridis