[Django] #22206: TextField doesn't set max_length attribute on its formfield, unlike CharField

4 views
Skip to first unread message

Django

unread,
Mar 4, 2014, 8:40:27 AM3/4/14
to django-...@googlegroups.com
#22206: TextField doesn't set max_length attribute on its formfield, unlike
CharField
-----------------------------+--------------------
Reporter: gcc | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------
In django/db/models/fields/__init__.py, the CharField field type
constructs its widgets with a max_length parameter, which ends up in the
widget:

{{{
class CharField(Field): # django.db.forms.fields
def formfield(self, **kwargs):
# Passing max_length to forms.CharField means that the value's
length
# will be validated twice. This is considered acceptable since we
want
# the value in the form field (to pass into widget for example).
defaults = {'max_length': self.max_length}
defaults.update(kwargs)
return super(CharField, self).formfield(**defaults)

class CharField(Field): # django.forms.fields
def widget_attrs(self, widget):
attrs = super(CharField, self).widget_attrs(widget)
if self.max_length is not None and isinstance(widget, TextInput):
# The HTML attribute is maxlength, not max_length.
attrs.update({'maxlength': str(self.max_length)})
return attrs
}}}

But nothing like this happens for the TextField database field, which
creates a TextField form field, which uses a Textarea widget by default:

{{{
class TextField(Field):
description = _("Text")

def formfield(self, **kwargs):
defaults = {'widget': forms.Textarea}
defaults.update(kwargs)
return super(TextField, self).formfield(**defaults)
}}}

I think that TextField doesn't override form_class, so it gets the default
CharField from Field.formfield(), as CharField does, but with a custom
widget. So all we'd need to do is set the attribute in the CharField db
field:

{{{
def formfield(self, **kwargs):
# Passing max_length to forms.CharField means that the value's
length
# will be validated twice. This is considered acceptable since we
want
# the value in the form field (to pass into widget for example).
defaults = {'max_length': self.max_length}
defaults.update(kwargs)
return super(TextField, self).formfield(**defaults)
}}}

and add the attribute to the widget in CharField:

{{{
def widget_attrs(self, widget):
attrs = super(CharField, self).widget_attrs(widget)
if self.max_length is not None: # all types of widgets, not hard
coded
# The HTML attribute is maxlength, not max_length.
attrs.update({'maxlength': str(self.max_length)})
return attrs
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22206>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 4, 2014, 9:20:29 AM3/4/14
to django-...@googlegroups.com
#22206: TextField doesn't set max_length attribute on its formfield, unlike
CharField
-----------------------------+--------------------------------------
Reporter: gcc | Owner: gcc
Type: New feature | Status: assigned
Component: Forms | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by gcc):

* status: new => assigned
* needs_better_patch: => 0
* needs_tests: => 0
* owner: nobody => gcc
* needs_docs: => 0
* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/2391

--
Ticket URL: <https://code.djangoproject.com/ticket/22206#comment:1>

Django

unread,
Mar 4, 2014, 2:35:32 PM3/4/14
to django-...@googlegroups.com
#22206: TextField doesn't set max_length attribute on its formfield, unlike
CharField
-----------------------------+------------------------------------

Reporter: gcc | Owner: gcc
Type: New feature | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by claudep):

* version: 1.6 => master
* stage: Unreviewed => Accepted


Comment:

Sure, `maxlength` is now also handled by `textarea` in HTML 5
(http://dev.w3.org/html5/markup/textarea.html#textarea.attrs.maxlength).

--
Ticket URL: <https://code.djangoproject.com/ticket/22206#comment:2>

Django

unread,
Mar 5, 2014, 4:13:12 AM3/5/14
to django-...@googlegroups.com
#22206: TextField doesn't set max_length attribute on its formfield, unlike
CharField
-----------------------------+------------------------------------
Reporter: gcc | Owner: gcc
Type: New feature | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------

Comment (by claudep):

The patch is now in good shape. However, I'd like to add an additional
note in the `TextField` documentation, something like this:
{{{
.. versionchanged:: 1.7

If you specify a ``max_length`` attribute, it will be reflected in the
:class:`~django.forms.Textarea` widget of the auto-generated form
field.
However it is not enforced at the model or database level. Use a
:class:`CharField` for that.
}}}
Opinion?

--
Ticket URL: <https://code.djangoproject.com/ticket/22206#comment:3>

Django

unread,
Mar 5, 2014, 5:15:19 AM3/5/14
to django-...@googlegroups.com
#22206: TextField doesn't set max_length attribute on its formfield, unlike
CharField
-----------------------------+------------------------------------
Reporter: gcc | Owner: gcc
Type: New feature | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------

Comment (by mjtamlyn):

Yes, I think that admonition is quite important.

--
Ticket URL: <https://code.djangoproject.com/ticket/22206#comment:4>

Django

unread,
Mar 5, 2014, 2:13:25 PM3/5/14
to django-...@googlegroups.com
#22206: TextField doesn't set max_length attribute on its formfield, unlike
CharField
-----------------------------+------------------------------------
Reporter: gcc | Owner: gcc
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by Claude Paroz <claude@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"95c74b9d699c29fe808684774548e2864d64665a"]:
{{{
#!CommitTicketReference repository=""
revision="95c74b9d699c29fe808684774548e2864d64665a"
Fixed #22206 -- Passed models.TextField.max_length to
forms.CharField.maxlength
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22206#comment:5>

Reply all
Reply to author
Forward
0 new messages