[Django] #23098: ugettext_lazy gets translated too early in modelForm field choices

26 views
Skip to first unread message

Django

unread,
Jul 25, 2014, 9:19:00 AM7/25/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
------------------------------+--------------------
Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Translations | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------
This ticket first was submitted against the pytest_django repository:
https://github.com/pelme/pytest_django/issues/135

However, I think the maintainer is right and it's a Django bug: because of
the new feature allowing to customize the "empty choice", the lazy
translation of named groups in a choice list for a modelForm field gets
translated at import time.

The steps to reproduce are in the ticket linked above, and consists in:
- have a choice list for a model field which contains name groups, with
the groups' names being translated (using ugettext_lazy)
- create a ModelForm for this model
- import the ModelForm from an external script that has the settings
configured, but not called `django.setup()` yet

I understand the steps to reproduce are really a corner case and may seem
irrelevant, but this bug means that we might have '''lazy translations
that are translated way to early (at import time) instead of staying
lazy'''.

With the normal Django usage, this will translate too early silently, and
not raise any error exception.

From what I understand, this happens in the code that checks if the choice
(in our case, the name of a group) is the empty choice:
https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L732

I can imagine two solutions to this issue:
1/ only do the check if the "choice" isn't a group name
2/ do something like `choice is None or isinstance(choice, six.text_type)
and choice == ''`

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

Django

unread,
Jul 25, 2014, 1:23:30 PM7/25/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
---------------------------------+------------------------------------

Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: master
Severity: Release blocker | 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 claudep):

* severity: Normal => Release blocker
* needs_better_patch: => 0
* component: Translations => Forms
* needs_tests: => 0
* needs_docs: => 0
* stage: Unreviewed => Accepted


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

Django

unread,
Jul 25, 2014, 1:24:19 PM7/25/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
---------------------------------+------------------------------------

Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: master
Severity: Release blocker | 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):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/2959

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

Django

unread,
Jul 25, 2014, 1:31:07 PM7/25/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
-------------------------------------+-------------------------------------

Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* stage: Accepted => Ready for checkin


Comment:

Should the version be 1.7?

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

Django

unread,
Jul 26, 2014, 9:10:45 AM7/26/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
-------------------------------------+-------------------------------------

Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.7-rc-1

Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* version: master => 1.7-rc-1


Comment:

Confirmed it's a regression in 1.7.

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

Django

unread,
Jul 26, 2014, 9:16:18 AM7/26/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
-------------------------------------+-------------------------------------
Reporter: magopian | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.7-rc-1
Severity: Release blocker | Resolution: invalid

Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* status: new => closed
* resolution: => invalid


Comment:

This report seems invalid to me. The reproduction instructions include:

> import the ModelForm from an external script that has the settings
configured, but not called django.setup() yet

But the 1.7 release notes explain that you should call `django.setup()`
before doing anything else.

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

Django

unread,
Jul 26, 2014, 9:25:36 AM7/26/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
-------------------------------------+-------------------------------------

Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.7-rc-1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* status: closed => new
* resolution: invalid =>


Comment:

Mmm, regardless, if the translation is resolved at import time, that's
bad. We should at least fix that.

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

Django

unread,
Jul 26, 2014, 2:43:57 PM7/26/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
-------------------------------------+-------------------------------------

Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.7-rc-1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by magopian):

Aymeric, I'm sorry that I confused things by talking about the pytest
usage in the STR, while it actually has nothing to do with the issue. It's
just that I discovered this issue in this particular case, while it was
just "failing" (translating to early) silently otherwise.

As you said, the issue is that the translation is resolved at import time,
and that's the point of this ticket (hence the title), sorry if I made it
harder to understand by providing to much (or confusing) details.

--
Ticket URL: <https://code.djangoproject.com/ticket/23098#comment:7>

Django

unread,
Jul 27, 2014, 7:25:32 AM7/27/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
-------------------------------------+-------------------------------------

Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.7-rc-1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by apollo13):

* cc: apollo13 (added)


Comment:

https://github.com/django/django/pull/2985 is the new PR

--
Ticket URL: <https://code.djangoproject.com/ticket/23098#comment:8>

Django

unread,
Jul 27, 2014, 7:32:24 AM7/27/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
-------------------------------------+-------------------------------------
Reporter: magopian | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.7-rc-1
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Florian Apolloner <florian@…>):

* status: new => closed

* resolution: => fixed


Comment:

In [changeset:"2f73b527dda6683868fac2791f7f07ccb01ea0d9"]:
{{{
#!CommitTicketReference repository=""
revision="2f73b527dda6683868fac2791f7f07ccb01ea0d9"
Fixed #23098 -- Checked that lazy choices are not evaluated too soon

Thanks Matthieu Agopian for the report.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23098#comment:9>

Django

unread,
Jul 27, 2014, 7:33:07 AM7/27/14
to django-...@googlegroups.com
#23098: ugettext_lazy gets translated too early in modelForm field choices
-------------------------------------+-------------------------------------
Reporter: magopian | Owner: nobody

Type: Bug | Status: closed
Component: Forms | Version: 1.7-rc-1
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Florian Apolloner <florian@…>):

In [changeset:"99c2c917c3f859f2cc3fc1f7d5abb72730aa6c48"]:
{{{
#!CommitTicketReference repository=""
revision="99c2c917c3f859f2cc3fc1f7d5abb72730aa6c48"
[1.7.x] Fixed #23098 -- Checked that lazy choices are not evaluated too
soon

Thanks Matthieu Agopian for the report.

Backport of 2f73b527dda6683868fac2791f7f07ccb01ea0d9 from master.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23098#comment:10>

Reply all
Reply to author
Forward
0 new messages