[Django] #20573: UnicodeEncodeError on (Generic)Fields

4 views
Skip to first unread message

Django

unread,
Jun 7, 2013, 12:11:06 PM6/7/13
to django-...@googlegroups.com
#20573: UnicodeEncodeError on (Generic)Fields
----------------------------------------------+----------------------------
Reporter: jayfk | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.5
Severity: Normal | Keywords:
Triage Stage: Unreviewed | UnicodeEncodeError
Easy pickings: 1 | Has patch: 0
| UI/UX: 0
----------------------------------------------+----------------------------
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()"

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

Django

unread,
Jun 7, 2013, 12:48:16 PM6/7/13
to django-...@googlegroups.com
#20573: UnicodeEncodeError on (Generic)Fields
-------------------------------------+-------------------------------------
Reporter: jayfk | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.5
(models, ORM) | Resolution: invalid
Severity: Normal | Triage Stage:
Keywords: UnicodeEncodeError | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

* status: new => closed
* needs_docs: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

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`.

--
Ticket URL: <https://code.djangoproject.com/ticket/20573#comment:1>

Django

unread,
Jun 8, 2013, 7:53:51 PM6/8/13
to django-...@googlegroups.com
#20573: UnicodeEncodeError on (Generic)Fields
------------------------------------+--------------------------------------
Reporter: jayfk | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: UnicodeEncodeError | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 1
------------------------------------+--------------------------------------
Changes (by ogpcludi <sample@…>):

* status: closed => new
* needs_better_patch: 0 => 1
* component: Database layer (models, ORM) => contrib.admin
* resolution: invalid =>
* needs_tests: 0 => 1
* version: 1.5 => master
* needs_docs: 0 => 1
* has_patch: 0 => 1
* ui_ux: 0 => 1
* type: Bug => Uncategorized


Comment:

1

--
Ticket URL: <https://code.djangoproject.com/ticket/20573#comment:3>

Django

unread,
Jun 8, 2013, 7:53:51 PM6/8/13
to django-...@googlegroups.com
#20573: UnicodeEncodeError on (Generic)Fields
------------------------------------+--------------------------------------
Reporter: jayfk | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: invalid

Keywords: UnicodeEncodeError | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 1
------------------------------------+--------------------------------------
Changes (by ogpcludi <sample@…>):

* needs_better_patch: 0 => 1


* component: Database layer (models, ORM) => contrib.admin

* needs_tests: 0 => 1
* version: 1.5 => master
* needs_docs: 0 => 1
* has_patch: 0 => 1
* ui_ux: 0 => 1
* type: Bug => Uncategorized


Comment:

1

--
Ticket URL: <https://code.djangoproject.com/ticket/20573#comment:2>

Django

unread,
Jun 8, 2013, 7:53:51 PM6/8/13
to django-...@googlegroups.com
#20573: UnicodeEncodeError on (Generic)Fields
------------------------------------+--------------------------------------
Reporter: jayfk | Owner: nobody

Type: Uncategorized | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: invalid
Keywords: UnicodeEncodeError | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 1
------------------------------------+--------------------------------------
Changes (by ogpcludi <sample@…>):

* needs_better_patch: 0 => 1
* component: Database layer (models, ORM) => contrib.admin
* needs_tests: 0 => 1
* version: 1.5 => master
* needs_docs: 0 => 1
* has_patch: 0 => 1
* ui_ux: 0 => 1
* type: Bug => Uncategorized


Comment:

1

--
Ticket URL: <https://code.djangoproject.com/ticket/20573#comment:4>

Django

unread,
Jun 8, 2013, 8:06:57 PM6/8/13
to django-...@googlegroups.com
#20573: UnicodeEncodeError on (Generic)Fields
------------------------------------+--------------------------------------
Reporter: jayfk | Owner: nobody
Type: Uncategorized | Status: new

Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: UnicodeEncodeError | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 1
------------------------------------+--------------------------------------

Comment (by ogpcludi <sample@…>):

1

--
Ticket URL: <https://code.djangoproject.com/ticket/20573#comment:6>

Django

unread,
Jun 8, 2013, 8:06:57 PM6/8/13
to django-...@googlegroups.com
#20573: UnicodeEncodeError on (Generic)Fields
------------------------------------+--------------------------------------
Reporter: jayfk | Owner: anonymous
Type: Uncategorized | Status: assigned

Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: UnicodeEncodeError | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 1
------------------------------------+--------------------------------------
Changes (by ogpcludi <sample@…>):

* status: new => assigned
* owner: nobody => anonymous


Comment:

1

--
Ticket URL: <https://code.djangoproject.com/ticket/20573#comment:5>

Reply all
Reply to author
Forward
0 new messages