﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28067	Clarify __str__() return type when using python_2_unicode_compatible()	Christophe Pettus	nobody	"The __str__ method (as implemented using six) strips off the ""unicodeness"" of a string. If chained together, this can result in an error:

{{{
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class A(models.Model):
    c = models.CharField(max_length=20)

    def __str__(self):
        return self.c


@python_2_unicode_compatible
class B(models.Model):
    a = models.ForeignKey(A)

    def __str__(self):
        return str(self.a)
}}}

{{{
>>> from test.models import A, B
>>> a = A(c=u'réparer')
>>> a.save()
>>> b = B(a=a)
>>> b.save()
>>> a
<A: réparer>
>>> b
<B: [Bad Unicode data]>
>>> print b
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/Users/xof/Documents/Dev/environments/peep/lib/python2.7/site-packages/django/utils/six.py"", line 842, in <lambda>
    klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
}}}

Note that the same behavior exists without @python_2_unicode_compatible, but the example uses so as to follow the correct path.

This is probably a bug in six (shouldn't __str__ preserve its unicode type in this case?), but we might want to ameliorate it."	Cleanup/optimization	closed	Documentation	dev	Normal	fixed			Accepted	1	0	0	0	0	0
