Opened 9 years ago

Closed 9 years ago

#20573 closed Bug (invalid)

UnicodeEncodeError on (Generic)Fields

Reported by: Jannis Gebauer Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords: UnicodeEncodeError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


First of all: All models are encoded/decoded properly using admin or by getting them with Model.objects.get()/all()/filter().
However, there seems to be a bug with generic Foreignkeys that are wired together to build a generic m2m table.

I could only reproduce this using an app called django-generic-m2m. I've read the code and there seems to be nothing special that is messing around with the ORM, so I think it's a django bug.


Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/var/www/", line 103, in detail
    print elem
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 433, in __str__
    return force_text(self).encode('utf-8')
  File "/usr/local/lib/python2.7/dist-packages/django/utils/", line 115, in force_text
    raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 4: ordinal not in range(128). You passed in <RelatedObject: [Bad Unicode data]> (<class 'genericm2m.models.RelatedObject'>)


title = models.CharField(max_length=200)
def __unicode__(self):
        return self.title

unicode method of genericm2m:


collation: utf8_unicode_ci

steps to reproduce:

- pip install django-generic-m2m
- add 'genericm2m', to installed apps
- create 2 models
- from genericm2m.models import RelatedObjectsDescriptor
- add related = RelatedObjectsDescriptor() to one of your models
- add a model instance that returns unicode characters
- model1.related.connect(model2)
- for elem in model1.related.all()
     print elem

some guy in irc suggested to upgrade to ".format()"

Change History (1)

comment:1 Changed 9 years ago by Claude Paroz

Resolution: invalid
Status: newclosed

That's a bug in django-generic-m2m __unicode__ method. It should return a unicode string, and currently it returns a bytestring.

It should be either return u'%s related to %s ("%s")' % (self.parent, self.object, self.alias) (notice the u prefix) or alternatively the file could import from __future__ import unicode_literals.

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