Opened 5 years ago

Closed 5 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: master
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 Changed 5 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 5 years ago by Rolo

Owner: changed from nobody to Rolo
Status: newassigned

comment:3 Changed 5 years ago by Rolo

Have created a pull request - https://github.com/django/django/pull/4751

comment:4 Changed 5 years ago by Kalle Bronsen

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

comment:6 Changed 5 years ago by Tim Graham <timograham@…>

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