Opened 18 years ago

Closed 18 years ago

#3996 closed (fixed)

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

Reported by: Ivan Sagalaev <Maniac@…> Owned by: Malcolm Tredinnick
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: no UI/UX: no

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

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

Download all attachments as: .zip

Change History (5)

comment:1 by Simon G. <dev@…>, 18 years ago

Keywords: unicode added
Triage Stage: UnreviewedAccepted

comment:2 by Malcolm Tredinnick, 18 years ago

Cc: Malcolm Tredinnick removed
Owner: changed from Jacob to Malcolm Tredinnick

by Ivan Sagalaev <Maniac@…>, 18 years ago

Attachment: 3996.diff added

Patch

comment:3 by Ivan Sagalaev <Maniac@…>, 18 years ago

Summary: [unicode] NodeList and smart_str prefer __str__ to __unicode__[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 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: newclosed

(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