Opened 17 years ago
Closed 17 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)
Change History (10)
comment:1 by , 17 years ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
comment:2 by , 17 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
comment:3 by , 17 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
comment:4 by , 17 years ago
Second patch is incorrect. It casts all values to strings. So, from True becomes 'True' and so on. First works better.
follow-up: 6 comment:5 by , 17 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.
follow-up: 7 comment:6 by , 17 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 , 17 years ago
Attachment: | proxy.2.diff added |
---|
comment:7 by , 17 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 , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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.