如何在django中设置表的char列的编码? [英] How to set the encoding for the tables' char columns in django?

查看:304
本文介绍了如何在django中设置表的char列的编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django编写的项目。应该存储一些字符串的所有字段都应该是UTF-8,但是当我运行

  manage.py syncdb 

所有相应的列都使用cp1252字符集创建(在哪里找到 - 我没有想法),我必须手动更新每一列...



有没有办法告诉Django创建所有这些列与UTF-8编码首先?



BTW,我使用MySQL。

解决方案

Django没有指定在 CREATE TABLE 语句中的字符集和排序规则。一切都由数据库字符集决定。执行 ALTER DATABASE ... CHARACTER SET utf8 COLLATE utf8_general_ci 在运行 syncdb 应该有帮助。



对于连接,Django自动发出 SET NAMES utf8 ,所以您不需要担心默认连接字符集设置。


I have a project written in Django. All fields that are supposed to store some strings are supposed to be in UTF-8, however, when I run

manage.py syncdb

all respective columns are created with cp1252 character set (where did it get that -- I have no idea) and I have to manually update every column...

Is there a way to tell Django to create all those columns with UTF-8 encoding in the first place?

BTW, I use MySQL.

解决方案

Django does not specify charset and collation in CREATE TABLE statements. Everything is determined by database charset. Doing ALTER DATABASE ... CHARACTER SET utf8 COLLATE utf8_general_ci before running syncdb should help.

For connection, Django issues SET NAMES utf8 automatically, so you don't need to worry about default connection charset settings.

这篇关于如何在django中设置表的char列的编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆