Django

Code

Ticket #3996 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

[unicode][patch] NodeList and smart_str prefer __str__ to __unicode__

Reported by: Ivan Sagalaev <Maniac@SoftwareManiacs.Org> Assigned to: mtredinnick
Milestone: Component: Uncategorized
Version: other branch Keywords: unicode
Cc: Maniac@SoftwareManiacs.Org Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

This is tricky one :-)

When a template is rendered its NodeList? does somethig like this:

''.join([smart_str(node, settings.DEFAULT_CHARSET) for node in nodes])

The smart_str converts any object to a str. Now, if an object is a model instance and that only __unicode__ method, smart_str doesn't use it at all and uses instead default Model.__str__. This leads to simple things like this:

{{ obj }}

to give out "ModelName? object" instead of meaningful things.

I'm not sure where to fix it:

  • change smart_str to look for __unicode__ and call it explicitly before str()
  • change NodeList? to do u''.join(...).decode(settings.DEFAULT_CHARSET)
  • (not a fix but masking) change Model.__str__ to look into __unicode__

I actually prefer to do all these items, they all seem logical. Except may be the first one since I don't understand completely the semantics of smart_str.

Attachments

3996.diff (472 bytes) - added by Ivan Sagalaev <Maniac@SoftwareManiacs.Org> on 04/21/07 13:56:14.
Patch

Change History

04/11/07 05:23:45 changed by Simon G. <dev@simon.net.nz>

  • keywords set to unicode.
  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

04/21/07 03:51:27 changed by mtredinnick

  • cc changed from mtredinnick, Maniac@SoftwareManiacs.Org to Maniac@SoftwareManiacs.Org.
  • owner changed from jacob to mtredinnick.

04/21/07 13:56:14 changed by Ivan Sagalaev <Maniac@SoftwareManiacs.Org>

  • attachment 3996.diff added.

Patch

04/21/07 13:59:20 changed by Ivan Sagalaev <Maniac@SoftwareManiacs.Org>

  • summary changed from [unicode] NodeList and smart_str prefer __str__ to __unicode__ to [unicode][patch] NodeList and smart_str prefer __str__ to __unicode__.

Added a (quite simple) patch. Malcolm, I think I see now why we shouldn't join unicode objects and convert afterwards. This is because you intend to leave users an option to define str for their models instead of unicode. This way we should explicitly use it.

04/21/07 22:13:01 changed by mtredinnick

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

(In [5057]) unicode: Fixed #3996. Added check for model-specific unicode method to default Model.str method. Thanks, Ivan Sagalaev.


Add/Change #3996 ([unicode][patch] NodeList and smart_str prefer __str__ to __unicode__)




Change Properties
Action