Opened 7 years ago

Closed 7 years ago

#5641 closed (fixed)

ugettext_lazy in model's default

Reported by: permon Owned by: permon
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 permon 7 years ago.
variant patch
proxy.2.diff (611 bytes) - added by permon 7 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 7 years ago by permon

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Ready for checkin

comment:2 Changed 7 years ago by mtredinnick

  • Patch needs improvement set
  • Triage Stage changed from Ready for checkin to Accepted

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 7 years ago by permon

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 7 years ago by permon

variant patch

comment:4 Changed 7 years ago by permon

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

comment:5 follow-up: Changed 7 years ago by permon

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 ; follow-up: Changed 7 years ago by 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.

Changed 7 years ago by permon

comment:7 in reply to: ↑ 6 Changed 7 years ago by permon

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 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(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