Django设置了存储引擎和默认字符集 [英] Django set Storage Engine & Default Charset
问题描述
从我的models.py
创建表格.我不知道该怎么做两件事-
Creating my tables from my models.py
. I donno how to do 2 things -
- 我想指定
MySQL
以将某些表创建为InnoDB
&一些作为MyISAM
.我该怎么办? - 我还想将表
DEFAULT CHARSET
指定为utf8
.我该怎么办?
- I want to specify
MySQL
to create some of my tables asInnoDB
& some asMyISAM
. How do I do it? - Also I want to specify my tables
DEFAULT CHARSET
asutf8
. How do I do it?
这是我运行syncdb
-
...
) ENGINE=MyISAM DEFAULT CHARSET=latin1
我使用Ubuntu 10.04,Django 1.2.X,MySQL 5.1.X
I use Ubuntu 10.04, Django 1.2.X, MySQL 5.1.X
更新:
我认为这些可能是MySQL的默认设置&我最终更改了my.cnf
,在其中添加了default-character-set = utf8
.但是没有用.
UPDATE:
I thought these might be MySQL default settings & I ended up changing my.cnf
where I added default-character-set = utf8
. But to no use.
推荐答案
我认为您不能逐表更改存储引擎,但可以逐个数据库地更改存储引擎.当然,这意味着例如InnoDB外键约束不能应用于MyISAM表的外键.
I don't think you can change storage engines on a table-by-table basis, but you can do it on a database-by-database basis. This, of course, means that InnoDB foreign key constraints, for example, can't apply to foreign keys to MyISAM tables.
因此,您需要声明两个数据库",它们很可能在同一服务器上:
So you need to declare two "databases", which may very well be on the same server:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
#...
}
'innodb': {
'ENGINE': 'django.db.backends.mysql',
#...
'OPTIONS': { 'init_command': 'SET storage_engine=INNODB;' }
}
}
您只需要将using('innodb')
应用于InnoDB领域中的表的查询集.
And you'll just need to apply using('innodb')
to querysets for tables in InnoDB land.
同样,对于UTF-8,我认为您需要在数据库级别执行此操作.我认为syncdb
不会为您创建数据库,而只是为表创建数据库.无论如何,您都应该手动创建数据库,这样就可以在运行syncdb
之前设置特权.您想要的数据库创建命令是:
As for UTF-8, again, I think you need to do this at the database level. I don't think syncdb
creates the database for you, just the tables. You should create the database manually anyway, so you can have privileges set right before running syncdb
. The database creation command you want is:
CREATE DATABASE django CHARACTER SET utf8;
也就是说,我通常建议人们在数据库中创建两个django用户:一个用于数据库架构工作("admin"),另一个用于其他所有事情(使用不同的密码):
That said, I usually recommend that people create two django users in the database: one for database schema work ("admin") and one for everything else (with different passwords):
CREATE DATABASE django CHARACTER SET utf8;
CREATE USER 'django_site'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE, DELETE ON django.* TO django_site;
CREATE USER 'django_admin'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE, DELETE ON django.* TO django_admin;
GRANT CREATE, DROP, ALTER, INDEX, LOCK TABLES ON django.* TO django_admin;
FLUSH PRIVILEGES;
(请注意,每个数据库都需要这样做.)
(Note that this needs to be done for each database.)
为此,您需要修改manage.py
:
import sys
if len(sys.argv) >= 2 and sys.argv[1] in ["syncdb", "dbshell", "migrate"]:
os.environ['DJANGO_ACCESS'] = "ADMIN"
然后在settings.py
中,使用环境变量选择正确的设置.确保该站点(即非管理员)用户是默认用户.
Then in your settings.py
, use the environment variable to pick the right settings. Make sure the site (i.e. non-admin) user is the default.
(此外,由于我的Django项目存储在Mercurial中,因此我不将数据库设置,SECRET_KEY
或其他任何敏感的内容存储在settings.py
中;我已settings.py
从可访问的外部文件中提取所有内容仅由Django的用户和服务器管理员来完成.我将如何做"作为练习留给读者...因为我在回答其他人的问题时详细介绍了其中的一些内容,而且我懒于查找它现在.)
(Additionally, I don't store the database setup, SECRET_KEY
, or anything else sensitive in settings.py
because my Django project is stored in Mercurial; I have settings.py
pull all that in from an external file accessible only by Django's user and the server admins. I'll leave the "how" as an exercise for the reader... because I detailed some of it in answers to others' questions, and I'm too lazy to look it up right now.)
这篇关于Django设置了存储引擎和默认字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!