标签:技术分享 size message import pre float margin running mod
上一篇我们已经完成数据库的设计,但是仅仅是python语言,并没有真正创建了数据库表。翻译成数据库语言,真正创建数据库表由django manage.py来实现,这一过程专业术语:迁移数据库
F:\pythoncode\django\workspace\blogproject (djanoproject_env) λ python manage.py makemigrations Migrations for ‘blog‘: blog\migrations\0001_initial.py: - Create model Category - Create model Post - Create model Tag - Add field tags to post
F:\pythoncode\django\workspace\blogproject
(djanoproject_env) λ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying blog.0001_initial... OK
Applying sessions.0001_initial... OK
F:\pythoncode\django\workspace\blogproject
(djanoproject_env) λ
python manage.py migrate
命令。Django 通过检测应用中 migrations\ 目录下的文件,得知我们对数据库做了哪些操作,然后它把这些操作翻译成数据库操作语言,从而把这些操作作用于真正的数据库。INSTALLED_APP
设置里看到这些应用,当然我们目前不必关心这些。blogproject/settings.py
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘blog‘,
对于了解数据库语言的人,你可以运行下面的命令看看 Django 究竟为我们做了什么:
python manage.py sqlmigrate blog 0001
你将看到输出了经 Django 翻译后的数据库表创建语句,这有助于你理解 Django ORM 的工作机制。
F:\pythoncode\django\workspace\blogproject (djanoproject_env) λ python manage.py sqlmigrate blog 0001 BEGIN; -- -- Create model Category -- CREATE TABLE "blog_category" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NO T NULL); -- -- Create model Post -- CREATE TABLE "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(70) NOT NU LL, "body" text NOT NULL, "create_time" datetime NOT NULL, "modified_time" date NOT NULL, "excerpt" v archar(200) NOT NULL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id"), "category_id" integ er NOT NULL REFERENCES "blog_category" ("id")); -- -- Create model Tag -- CREATE TABLE "blog_tag" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(70) NOT NULL ); -- -- Add field tags to post -- CREATE TABLE "blog_post_tags" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "post_id" integer NOT NULL REFERENCES "blog_post" ("id"), "tag_id" integer NOT NULL REFERENCES "blog_tag" ("id")); CREATE INDEX "blog_post_4f331e2f" ON "blog_post" ("author_id"); CREATE INDEX "blog_post_b583a629" ON "blog_post" ("category_id"); CREATE UNIQUE INDEX "blog_post_tags_post_id_4925ec37_uniq" ON "blog_post_tags" ("post_id", "tag_id"); CREATE INDEX "blog_post_tags_f3aa1999" ON "blog_post_tags" ("post_id"); CREATE INDEX "blog_post_tags_76f094bc" ON "blog_post_tags" ("tag_id"); COMMIT; F:\pythoncode\django\workspace\blogproject
[python][django学习篇][4]django完成数据库代码翻译:迁移数据库(migration)
标签:技术分享 size message import pre float margin running mod
原文地址:http://www.cnblogs.com/liuzhipenglove/p/7338549.html