Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#19413 closed Bug (invalid)

[model].__str__() disagrees with __unicode__() using python3 / django1.5b1

Reported by: xanthraxoid@… Owned by: nobody
Component: Python 3 Version: 1.5-beta-1
Severity: Normal Keywords: str unicode admin
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I created a few models, providing each with a unicode() method as described at [1] and in the admin area, when I look at a list of such objects, each is listed as [Class Name] object rather than the text representation defined in my unicode() methods.

I used python2.7 and got the expected results, I also defined str() to return self.unicode() and again got the expected results.

I assume this is something to do with python3's handling of unicode/byte-strings, but it seems to me that one of the following three is needed:

1) the str() function should continue to return the same text representation as the unicode() method unless overridden
2)the admin should use unicode() in place of str() - along with any other part of django
3) the docs should be updated to make it clear what I should do in this situation (I'll stick with defining str() to return the same as unicode() for now, but I'd rather not have to, DRY and all that...

[1]
https://docs.djangoproject.com/en/dev/intro/tutorial01/
https://docs.djangoproject.com/en/dev/topics/db/models/

NB eveywhere you see unicode(), the bug tracker software has helpfully converted my double underlines before and after the function name into underlining the function name - sorry!

Cheers & God bless

Sam "SammyTheSnake" Penny

Change History (3)

comment:1 by Aymeric Augustin, 12 years ago

Resolution: invalid
Status: newclosed

The tutorial is currently written for Python 2 only. The differences between Python 2 and 3 aren't the first thing a beginner should be exposed to, and it's still too early for a Python 3-only tutorial.

If you're targeting Pyhon 3 only, define __str__. There's no such thing as __unicode__ methods in Python 3.

If you're targeting Python 2 and 3, look at the Python 3 docs and specifically the @python_2_unicode_compatible decorator.

comment:2 by thisgenericname@…, 12 years ago

I'm learning both Python (3.2) and Django (1.5b2) myself, but I've seem some possibly inconsistent behavior here myself:

If I only define unicode, the list of items in the admin interface will show the default ("Foo object") name, but when editing the object itself the breadcrumb trail will display "Home › FooApp › Foo › Test 222, Test".

If I define just str, or define both methods, the admin interface always shows the correct result.

This suggests to me that something might be a little off under the hood. Furthermore, the tutorial cited in [1] specifically has a sidebar for "Why unicode and not str", so there's already a place to mention Python 3 vs Python 2 semantics if necessary.

comment:3 by Aymeric Augustin, 12 years ago

__unicode__ is a Python 2-only construct. Everything works correctly under Python 3 when you define __str__. That's the expected result and I'm positive that the code is fine under the hood

The differences between Python 2 and 3 don't stop there. I decided against covering both versions in the tutorial, because there's just too much to explain. At this stage, if you're beginning with Python and following Django's tutorial, I strongly recommend using Python 2.

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