Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#27901 closed Bug (wontfix)

Unicode subclasses can't be saved to TextField with MySQL and Python 2

Reported by: Dominique Barton Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords: mysql, unicode, error
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Dominique Barton)

Hi there

I use a custom model field (inherited from TextField) which simply returns its value as an "enhanced" unicode object.
Unfortunately, this will raise UnicodeErrors as MySQLdb apparently fails recognizing those values as unicode. The comparision isn't very clever there ;)

MySQLdb has a use_unicode flag, but I don't know how to implement it in Django and I think this is a serious bug.

I've prepared a whole test case right here: https://github.com/domibarton/django-mysql-unicode-error

I hope this is enough for a "proper" bug description.

Cheers
Domi

EDIT: It has NOTHING to do with my custom text field, this was just an example. Basically Django (or MySQLdb) fails when writing non-unicode objects (but unicode-inherited objects) values to a TextField. Proof for that issue is right here: https://gist.github.com/domibarton/aa691342321f63f3511bb1ff53b946c7

Change History (8)

comment:1 by Tim Graham, 7 years ago

Resolution: needsinfo
Status: newclosed

In this case, you need to explain why Django is at fault and ideally provide a patch that fixes the issue. From what you provided so far, it looks like you're asking for help debugging your custom field. For ways to get help with that, see TicketClosingReasons/UseSupportChannels.

comment:3 by Dominique Barton, 7 years ago

I was in the IRC channels and they sent me here. I specifically asked them "is this really OK to file a bug" and the answer was "yes it is".
Now you tell me I've to go back?

in reply to:  1 comment:4 by Dominique Barton, 7 years ago

Resolution: needsinfo
Status: closednew

Replying to Tim Graham:

From what you provided so far, it looks like you're asking for help debugging your custom field.

No I don't need help with debugging my custom field as my custom field is not the problem! As I described before, IT IS A BUG and therefore I think this ticket is quite valid!

Let's keep it simple and ignore the fact that I'm trying to make a custom model field. Use a stock TextField, write a value to it and then save the model. Now do the same, but use a custom unicode class for the value. Something like that:

https://gist.github.com/domibarton/aa691342321f63f3511bb1ff53b946c7

comment:5 by Dominique Barton, 7 years ago

Description: modified (diff)
Summary: Problem with Django & MySQL & model fields returning custom unicode classesProblem with Django models & MySQL & custom unicode classes

comment:6 by Dominique Barton, 7 years ago

Additional info:

Switching from mysql-python to mysqlcient didn't help.

However, switching from python2 to python3 solves the issue.
This might be because in py3 everything is unicode by default (vs. byte strings in py2) and the "default" type is unicode.

I'll try to migrate my project to py3 and the bug is gone. Unfortunately it still exists with py2.

comment:7 by Tim Graham, 7 years ago

Resolution: wontfix
Status: newclosed
Summary: Problem with Django models & MySQL & custom unicode classesUnicode subclasses can't be saved to TextField with MySQL and Python 2

Even if Django is at fault (it's still not clear to me if that's the case), the time for fixing issues that only affect Python 2 has passed since Django's master branch only supports Python 3.

in reply to:  7 comment:8 by Dominique Barton, 7 years ago

Replying to Tim Graham:

Even if Django is at fault (it's still not clear to me if that's the case), the time for fixing issues that only affect Python 2 has passed since Django's master branch only supports Python 3.

Didn't know that. OK, I'll try to move everything to py3.

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