Opened 9 years ago

Closed 9 years ago

#5641 closed (fixed)

ugettext_lazy in model's default

Reported by: Tomáš Kopeček Owned by: Tomáš Kopeček
Component: Internationalization Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

Description

When you pass ugettext_lazy as an argument of field default, then in non-interactive creation you will always get proxy as db field value. The problem is not seen when new instance is created interactively (e.g. in Admin). In such case coercing to unicode works well. Because in documentation is said, that ugettext_lazy should be used in models everywhere then we should be aware of such cases.

Attachments (2)

proxy.diff (571 bytes) - added by Tomáš Kopeček 9 years ago.
variant patch
proxy.2.diff (611 bytes) - added by Tomáš Kopeček 9 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 9 years ago by Tomáš Kopeček

Triage Stage: UnreviewedReady for checkin

comment:2 Changed 9 years ago by Malcolm Tredinnick

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

I suspect this patch should possibly just be a call to force_unicode(), but to ensure I understand it, could you give an example of the type of problem you are seeing? It's not clear to me how to repeat whatever the bug is.

comment:3 Changed 9 years ago by Tomáš Kopeček

class TestModel(models.Model):
    text = models.CharField(maxlength = 100, default = ugettext_lazy('Anything'))

tm = TestModel()
tm.save()

Creating model is ok, but in tm.text is proxy object and during tm.save() it raises error. Type of error is dependent on db backend. With MySQL it raises something like 'value too long, truncated', with sqlite3 (exactly this example)

InterfaceError: Error binding parameter 0 - probably unsupported type.

About force_unicode - it is also solution, I am attaching variant patch

Changed 9 years ago by Tomáš Kopeček

Attachment: proxy.diff added

variant patch

comment:4 Changed 9 years ago by Tomáš Kopeček

Second patch is incorrect. It casts all values to strings. So, from True becomes 'True' and so on. First works better.

comment:5 Changed 9 years ago by Tomáš Kopeček

From this flows another idea: Maybe it could be good to add type bool to nonconverted types in force_unicode. I don't know how these types were selected, but I think it shoudl be there.

comment:6 in reply to:  5 ; Changed 9 years ago by Malcolm Tredinnick

Replying to permon:

Maybe it could be good to add type bool to nonconverted types in force_unicode.

The boolean type is already covered in the things that "strings_only" will ignore. In Python 2, a bool is an instance of int, so isinstance(True, int) is always true.

Changed 9 years ago by Tomáš Kopeček

Attachment: proxy.2.diff added

comment:7 in reply to:  6 Changed 9 years ago by Tomáš Kopeček

Replying to mtredinnick:

Replying to permon:

Maybe it could be good to add type bool to nonconverted types in force_unicode.

The boolean type is already covered in the things that "strings_only" will ignore. In Python 2, a bool is an instance of int, so isinstance(True, int) is always true.

In this case should be working last patch. I believe there will be no more variants :-)

comment:8 Changed 9 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [6453]) Fixed #5641 -- Handle lazy translations correctly when used as default arguments. Thanks, permon.

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