Search code examples
djangodjango-modelsdjango-userspython-unicode

Store unicode first_name & last_name in Django user record


Trying:

new_user = User.objects.create_user(username, email, password)
new_user.first_name = first_name
new_user.last_name = last_name
new_user.save()

but if first or last name is foreign accented letters (unicode?) I get garbage in the user's record.

What can I do?


Solution

  • Django supports unicode strings for User model, but also your database must support it. If you're using sqlite there must not be a problem, but for example in mySQL, default column encoding is not utf-8.

    To solve the problem, you can manually change the auth_user table columns first_name and last_name collation to utf8_unicode_ci. Alternatively you can set database collation to utf8_unicode_ci before doing syncdb (and creating tables for the fist time), this way all your tables and columns follow same encoding.