paweloque
paweloque

Reputation: 18864

Django widget for a dropdown list

Which django widget must be used for a dropdown list? I already have a model which provides a dropdown list. It is, however, necessary to customize the corresponding form element (text and error msg text) and it becomes necessary to specify the widget.

Here is the Model:

class ClientDetails(models.Model):
    paymentType = models.CharField(max_length=4, verbose_name='Zahlungsart', choices=PAYMENT_TYPES)

And the Form:

class ClientDetailsForm(ModelForm):
    paymentType = forms.???(label='Zahlungsart', error_messages={'required': (u'Waehlen Sie die Zahlungsart!'), 'invalid': (u'Waehlen Sie die Zahlungsart!')})

Upvotes: 3

Views: 23290

Answers (2)

nofoobar
nofoobar

Reputation: 3215

We need to use forms.Select()

class Meta:
    model=someForm
    fields=['Dropdown']
    widgets = {
      'Dropdown': forms.Select(attrs={'id':'choicewa'}),
      }

Upvotes: 6

Victor Miroshnikov
Victor Miroshnikov

Reputation: 594

The place you marked by ??? is for specifying Field class. If you want to specify proper field class you should use forms.ChoiceField.

Detailed information on widgets and fields:

Upvotes: 5

Related Questions