Opened 10 years ago

Closed 9 years ago

#24677 closed Bug (fixed)

models.TextField cleaning doesn't behave the same as models.CharField.

Reported by: Keryn Knight Owned by: Rolo
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: django@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For brevity's sake, I'm avoiding constructing whole model instances below, but the issue remains the same when they are attached.

>>> from django.db.models.fields import CharField, TextField
>>> CharField().to_python(1)
u'1'   # type: unicode
>>> TextField().to_python(1)
1   # type: int

I'd expect that cleaning a TextField ought to result in a str/promise/smart_text as with CharField. The issue arises I believe because TextField implements `get_prep_value` but not to_python, while CharField implements to_python which is called by get_prep_value. Both fields seem to exhibit the same behaviour for the get_prep_value:

>>> CharField().get_prep_value(1)
u'1'  # type: unicode
>>> TextField().get_prep_value(1)
u'1'  # type: unicode

From a cursory play in my toy project, it seems like hoisting the TextField.get_prep_value logic into to_python and calling it from get_prep_value like CharField does would make the behaviour consistent.

Change History (6)

comment:1 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Rolo, 9 years ago

Owner: changed from nobody to Rolo
Status: newassigned

comment:4 by Kalle Bronsen, 9 years ago

The test in that PR is using a CharField, however, its docstring mentions TextField.

comment:6 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 19e67c6c:

Fixed #24677 -- Made TextField.to_python() return a string.

This is consistent with CharField.

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