Opened 9 years ago

Closed 8 years ago

#3996 closed (fixed)

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

Reported by: Ivan Sagalaev <Maniac@…> Owned by: mtredinnick
Component: Uncategorized Version: other branch
Severity: Keywords: unicode
Cc: Maniac@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


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 (1)

3996.diff (472 bytes) - added by Ivan Sagalaev <Maniac@…> 8 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 9 years ago by Simon G. <dev@…>

  • Keywords unicode added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 8 years ago by mtredinnick

  • Cc mtredinnick removed
  • Owner changed from jacob to mtredinnick

Changed 8 years ago by Ivan Sagalaev <Maniac@…>


comment:3 Changed 8 years ago by Ivan Sagalaev <Maniac@…>

  • 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.

comment:4 Changed 8 years ago by mtredinnick

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

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

Note: See TracTickets for help on using tickets.
Back to Top