[Django] #24677: models.TextField cleaning doesn't behave the same as models.CharField.

11 views
Skip to first unread message

Django

unread,
Apr 21, 2015, 3:57:27 AM4/21/15
to django-...@googlegroups.com
#24677: models.TextField cleaning doesn't behave the same as models.CharField.
----------------------------------------------+--------------------
Reporter: kezabelle | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
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
[https://github.com/django/django/blob/b5e0eede406633b88c6222031171f80876d8f5e1/django/db/models/fields/__init__.py#L2121
TextField implements `get_prep_value`] but not `to_python`, while
`CharField` implements
[https://github.com/django/django/blob/b5e0eede406633b88c6222031171f80876d8f5e1/django/db/models/fields/__init__.py#L1105
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 [https://github.com/kezabelle/django-
strictmodels/commit/1acad9ef63769fc6378c6d023333259c2a8cc41f#diff-
4a6a4e7167b55119eefda4da703221edR160 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.

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

Django

unread,
Apr 21, 2015, 12:23:35 PM4/21/15
to django-...@googlegroups.com
#24677: models.TextField cleaning doesn't behave the same as models.CharField.
-------------------------------------+-------------------------------------

Reporter: kezabelle | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


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

Django

unread,
Jun 4, 2015, 6:02:34 AM6/4/15
to django-...@googlegroups.com
#24677: models.TextField cleaning doesn't behave the same as models.CharField.
-------------------------------------+-------------------------------------
Reporter: kezabelle | Owner: rolo
Type: Bug | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => assigned
* owner: nobody => rolo


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

Django

unread,
Jun 4, 2015, 6:37:41 AM6/4/15
to django-...@googlegroups.com
#24677: models.TextField cleaning doesn't behave the same as models.CharField.
-------------------------------------+-------------------------------------
Reporter: kezabelle | Owner: rolo
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by rolo):

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

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

Django

unread,
Jun 4, 2015, 7:06:10 AM6/4/15
to django-...@googlegroups.com
#24677: models.TextField cleaning doesn't behave the same as models.CharField.
-------------------------------------+-------------------------------------
Reporter: kezabelle | Owner: rolo
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by bronsen):

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

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

Django

unread,
Jun 4, 2015, 7:21:31 AM6/4/15
to django-...@googlegroups.com
#24677: models.TextField cleaning doesn't behave the same as models.CharField.
-------------------------------------+-------------------------------------
Reporter: kezabelle | Owner: rolo
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by bronsen):

alright, now the test uses TextField:
https://github.com/wildfish/django/commit/461473873131141955882318aa577d5ba9de30bc

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

Django

unread,
Jun 4, 2015, 9:04:31 AM6/4/15
to django-...@googlegroups.com
#24677: models.TextField cleaning doesn't behave the same as models.CharField.
-------------------------------------+-------------------------------------
Reporter: kezabelle | Owner: rolo
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

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

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

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


Comment:

In [changeset:"19e67c6cd13d6442abb2a6eba1b7fba80dcf5457" 19e67c6c]:
{{{
#!CommitTicketReference repository=""
revision="19e67c6cd13d6442abb2a6eba1b7fba80dcf5457"
Fixed #24677 -- Made TextField.to_python() return a string.

This is consistent with CharField.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24677#comment:6>

Reply all
Reply to author
Forward
0 new messages