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='Z开发者_运维百科ahlungsart', 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!')})
We need to use forms.Select()
class Meta:
model=someForm
fields=['Dropdown']
widgets = {
'Dropdown': forms.Select(attrs={'id':'choicewa'}),
}
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:
- Overriding the default field types or widgets
- Specifying widgets
精彩评论