Opened 16 years ago

Closed 16 years ago

#5641 closed (fixed)

ugettext_lazy in model's default

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

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 16 years ago.
variant patch
proxy.2.diff (611 bytes ) - added by Tomáš Kopeček 16 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 by Tomáš Kopeček, 16 years ago

Triage Stage: UnreviewedReady for checkin

comment:2 by Malcolm Tredinnick, 16 years ago

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 by Tomáš Kopeček, 16 years ago

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

by Tomáš Kopeček, 16 years ago

Attachment: proxy.diff added

variant patch

comment:4 by Tomáš Kopeček, 16 years ago

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

comment:5 by Tomáš Kopeček, 16 years ago

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.

in reply to:  5 ; comment:6 by Malcolm Tredinnick, 16 years ago

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.

by Tomáš Kopeček, 16 years ago

Attachment: proxy.2.diff added

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

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 by Malcolm Tredinnick, 16 years ago

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