Mysql + Unicode + username curiosity

8 views
Skip to first unread message

Nuno Maltez

unread,
Nov 14, 2011, 1:31:34 PM11/14/11
to django...@googlegroups.com
So, I just ran into a funny issue. I'm using Django + Mysql and when
retrieving users (Auth.user) form the database, the username field
comes as a bytestring while everything else seems to
come as unicode strings:

>>> from django.contrib.auth.models import User
>>> a=User.objects.get(id=1)
>>> a.username
'admin'
>>> a.email
u'admin@localhost'

I know you should only use letters and a few more characters in
usernames, but that seems to be verified in the forms only. I look at
the User model and username, email first and last names
are all CharFields, yet username displays this peculiar behaviour. Any
reason for this?

I'm using:
mysqld Ver 5.1.41
Django (1, 3, 1, 'final', 0)
MySQLdb (1,2,3,'final',0)


In my.conf used in the connection I have
[client]
default-character-set = utf8

and the tables were created with utf8_general_ci

Thanks in advance,
Nuno

Kurtis Mullins

unread,
Nov 14, 2011, 2:40:53 PM11/14/11
to django...@googlegroups.com
Hey,

I'm not sure on what the problem is here. I just wanted to let you know that I did the same test on my setup. And it appears to be returning the username as a unicode string.

>>> a = User.objects.get(id = 1)
>>> a.username
u'admin'

I don't have any default-character-set option in my my.cnf file.
My User table, and presumably all of my tables, are using the following collation:

Collation latin1_swedish_ci

Good luck!


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Tomasz Zieliński

unread,
Nov 14, 2011, 5:15:45 PM11/14/11
to django...@googlegroups.com
You might want to check the documention: https://docs.djangoproject.com/en/1.3/ref/databases/#creating-your-database

There seem to be issues like this one: "The main thing to be aware of in this case is that if you are using MySQLdb 1.2.2, the database backend in Django will then return bytestrings (instead of unicode strings) for any character fields it receive from the database." so it's possible that you also suffer from some configuration problem.


-- 
Tomasz Zielinski
Reply all
Reply to author
Forward
0 new messages