Reputation: 55
For instance, I have this code. How can I add the empty_label to the field Select.
class NameForm(forms.ModelForm):
class Meta:
model = Model
fields = ['choice',]
widgets = {
'choice': forms.Select(attrs={'class': 'class'}, ??empty_label='lorem'??),
}
models.py
class Book(models.Model):
choice = models.ForeignKey('Another Model', on_delete=models.PROTECT, null=True)
Upvotes: 2
Views: 545
Reputation: 477308
This is a parameter of the form field, not the widget. If you do not want to override the rest of the form field, you specify this in the constructor of the form:
class NameForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['choice'].empty_label = 'lorem'
class Meta:
model = Model
fields = ['choice',]
widgets = {
'choice': forms.Select(attrs={'class': 'class'}),
}
Upvotes: 2