Opened 5 years ago

Closed 5 years ago

#30831 closed Bug (invalid)

Django doesn't respect overriding the maxlength widget attribute for CharFields in ModelForm.

Reported by: Arsenal591 Owned by: nobody
Component: Forms Version: 2.2
Severity: Normal Keywords: form, modelform
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have the following field in model:

short_description = models.CharField(max_length=205)

and the widget in ModelForm:

'short_description': forms.Textarea(attrs={'rows': '3', 'minlength': 250,'maxlength': 250})

The issue:

In HTML, input, Django adds correctly minlength value, but for the maxlength value he get the value from the model field and not the one specified in the widget.

<textarea name="short_description" cols="40" rows="3"  minlength="250" maxlength="205" required="" id="id_short_description"></textarea>

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: invalid
Status: newclosed
Summary: Django doesn't respect the maxlength widget attributeDjango doesn't respect overriding the maxlength widget attribute for CharFields in ModelForm.

Thanks for this report, however this is a documented and expected behavior (see Field types and Overriding the default fields):

... Similarly, fields defined declaratively do not draw their attributes like max_length or required from the corresponding model. If you want to maintain the behavior specified in the model, you must set the relevant arguments explicitly when declaring the form field.

Note: See TracTickets for help on using tickets.
Back to Top