How to make tables generated by Django be utf8 encoded by default?

360 views
Skip to first unread message

zhou.h...@gmail.com

unread,
Oct 25, 2006, 6:01:01 AM10/25/06
to Django users
Hi,
I want to store Chinese characters in my MySQL 4.1.11 through Django
but unluckily, like most novices, I get totally mess characters when
displaying them on utf-8 encoded templates.
After some investigation I found that the tables created by 'manage
syncdb' are actually encoded by 'latin-1'. After changing the character
set manually(using the 'alter table xxx convert to character set
'utf8''), the characters were finely displayed:-)

So, do I have to change my table encoding manually every time after
performing 'manage syncdb'? That must be very painful:(
Thanks

Best Regards,
Lin

Malcolm Tredinnick

unread,
Oct 25, 2006, 9:38:21 PM10/25/06
to django...@googlegroups.com

If you want to run any set of SQL commands each time you create the
tables for a model, Django has support for this. Have a look at
http://www.djangoproject.com/documentation/model_api/#providing-initial-sql-data . That talks about providing some initial data for your tables, but you can include anything in there, including "ALTER TABLE..." statements. In fact, you probably want to set this up using the section immediately after the one I linked to -- where you can ensure the commands only run for MySQL.

Regards,
Malcolm


orestis

unread,
Oct 26, 2006, 7:48:32 AM10/26/06
to Django users
Here is a proposal: Why don't these SQL files generated by default by
the django-admin.py startproject utility ? They would start empty, with
just a comment:

--place initial data commands here, alter tables, whatever

Also, since django uses utf-8 from top to bottom, it makes sense to
make utf-8 an option for the database, or at least be able to specify
it in a Meta directive in a model class...

Should I post this in a ticket ? Hey, maybe I'll even write a patch
about it! I'm so excited about django, I want to give back!

Andy Dustman

unread,
Oct 27, 2006, 10:35:46 AM10/27/06
to django...@googlegroups.com

A couple options:

In your MySQL server configuration, you can set default-character-set
= utf8 and then this will be used for your connection any any tables
you create.

Another option is to set the default character set on the database
with CREATE DATABASE xxx CHARACTER SET UTF8.

http://dev.mysql.com/doc/refman/5.0/en/alter-database.html

You want to do this prior to creating tables, of course. ALTER
DATABASE can be uased to set the default on an existing database, but
doesn't affect existing tables.
--
This message has been scanned for memes and
dangerous content by MindScanner, and is
believed to be unclean.

Pythoni

unread,
Oct 27, 2006, 10:45:12 AM10/27/06
to Django users


I have also a problem with MySQL utf8 coding
I use MySQL database with utf8 character set and utf8_czech_ci
collation.
It works well on Linux server( with mod_python) but when I try to
export the data and import into the same Django
application but running on XP machine with runserver ,the utf8 is
gone.Instead of a proper coding there are some strange characters.

How can I solve the problem?

Reply all
Reply to author
Forward
0 new messages