In the Model
I set the maxlength of the field:
short_description = models.CharField(max_length=405)
In the ModelForm
in the widget attributes I set minlength an maxlength:
class ItemModelForm(forms.ModelForm):
class Meta:
model = Item
fields = ['name', 'short_description', 'description']
widgets = {
'name': TextInputWidget(attrs={'placeholder': 'Name*'}),
'short_description': TextareaWidget(attrs={'minlength': 200, 'maxlength': 400})
}
The issue in the HTML the minlegth is from the widget, but maxlength is still taken from the model(405 instead of 400).
I want the widget attribute to overwrite the model attribute.
The widgets
property in the Meta class modify only, well, the widgets, not the field attributes itself. What you have to do is redefine your model form field.
class ItemModelForm(forms.ModelForm):
short_description = forms.CharField(max_length=400, min_length=200, widget=TextareaWidget())
class Meta:
model = Item
fields = ['name', 'short_description', 'description']
widgets = {
'name': TextInputWidget(attrs={'placeholder': 'Name*'}),
}