Django

Code

Ticket #5641 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

ugettext_lazy in model's default

Reported by: permon Assigned to: permon
Milestone: Component: Internationalization
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

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

proxy.diff (0.6 kB) - added by permon on 10/03/07 04:13:45.
variant patch
proxy.2.diff (0.6 kB) - added by permon on 10/03/07 08:41:22.

Change History

09/30/07 09:41:33 changed by permon

  • needs_better_patch changed.
  • stage changed from Unreviewed to Ready for checkin.
  • needs_tests changed.
  • needs_docs changed.

10/02/07 19:52:57 changed by mtredinnick

  • needs_better_patch set to 1.
  • 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.

10/03/07 04:13:25 changed 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

10/03/07 04:13:45 changed by permon

  • attachment proxy.diff added.

variant patch

10/03/07 08:11:31 changed by permon

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

(follow-up: ↓ 6 ) 10/03/07 08:14:23 changed 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.

(in reply to: ↑ 5 ; follow-up: ↓ 7 ) 10/03/07 08:30:04 changed 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.

10/03/07 08:41:22 changed by permon

  • attachment proxy.2.diff added.

(in reply to: ↑ 6 ) 10/03/07 08:42:03 changed 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 :-)

10/03/07 20:55:51 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #5641 (ugettext_lazy in model's default)




Change Properties
Action