﻿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
20573	UnicodeEncodeError on (Generic)Fields	Jannis Gebauer	nobody	"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:
 

{{{
Traceback (most recent call last):
  File ""/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py"", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File ""/var/www/site.com/django/products/views.py"", line 103, in detail
    print elem
  File ""/usr/local/lib/python2.7/dist-packages/django/db/models/base.py"", line 433, in __str__
    return force_text(self).encode('utf-8')
  File ""/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py"", 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'>)
}}}

 
#-----
model:
 

{{{
title = models.CharField(max_length=200)
 
def __unicode__(self):
        return self.title
}}}

 
#----
unicode method of genericm2m:
 

{{{
https://github.com/coleifer/django-generic-m2m/blob/master/genericm2m/models.py#L229
}}}

 
#----

{{{
DB: MySQL
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()""
"	Bug	closed	Database layer (models, ORM)	1.5	Normal	invalid	UnicodeEncodeError		Unreviewed	0	0	0	0	1	0
