#28067: Encoding error when __str__ returns non-ASCII
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  Christophe Pettus                  |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  1.11
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/28067>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/046.6cffd4f02d6b81b915a0f66804077841%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to