You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Migrations for 'blog':
0001_initial.py:
- Create model Post
在 blog 这个 app 下建立了一个 migrations 文件夹,创建了 0001_initial.py 文件,其中记录了所有 models.py 的改动(创建了 Post 模块)
python manage.py sqlmigrate blog 0001 查看该 migrations 对于于什么样的 sql 语句
显示如下:
BEGIN;
--
-- Create model Post
--
CREATE TABLE "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(250) NOT
NULL, "slug" varchar(250) NOT NULL, "body" text NOT NULL, "publish" datetime NOT NULL, "created" dat
etime NOT NULL, "updated" datetime NOT NULL, "status" varchar(10) NOT NULL, "author_id" integer NOT
NULL REFERENCES "auth_user" ("id"));
CREATE INDEX "blog_post_2dbcba41" ON "blog_post" ("slug");
CREATE INDEX "blog_post_4f331e2f" ON "blog_post" ("author_id");
COMMIT;
python manage.py migrate 将之前的改动同步到数据库文件
显示如下:
Operations to perform:
Apply all migrations: admin, blog, contenttypes, auth, sessions
Running migrations:
Rendering model states... DONE
Applying blog.0001_initial... OK
如果之后 models 有新的改动,需要再次调用 makemigrations 命令
Creating an administration site for your models
利用 django.contrib.admin, 已经包括在 INSTALLED_APPS 中
Creating a superuser
创建管理员,python manage.py createsuperuser
显示如下:
Username (leave blank to use 'administrator'): admin
Email address: [email protected]
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Password:
Password (again):
This password is too common.
Password:
Password (again):
Superuser created successfully.
0x01 Django
Build a Blog Application
Post
模块解析title
:post title
字段,字符型字段,数据库中用于转换slug
: 在URLs
中使用author
pytz
模块timezone
,SQLite
处理日期时间时需要用到settings.py
中可以利用USE_TZ
命令设置激活或者不激活时区功能Activating your application
settins.py
中的INSTALLED_APPS
内添加blog
应用Creating and applying migrations
migrations
数据迁移工具migrate
用于执行迁移动作makemigrations
基于当前的model
创建新的迁移策略文件sqlmigrate
显示迁移的SQL
语句migration
是基于APP
的, 所以可以针对某些APP
不启用migration
功能返回到项目根目录,输入以下命令
python manage.py makemigrations blog
在
blog
这个app
下建立了一个migrations
文件夹,创建了0001_initial.py
文件,其中记录了所有models.py
的改动(创建了Post
模块)python manage.py sqlmigrate blog 0001
查看该migrations
对于于什么样的sql
语句显示如下:
python manage.py migrate
将之前的改动同步到数据库文件显示如下:
如果之后
models
有新的改动,需要再次调用makemigrations
命令Creating an administration site for your models
利用
django.contrib.admin
, 已经包括在INSTALLED_APPS
中Creating a superuser
创建管理员,
python manage.py createsuperuser
显示如下:
python manage.py runserver
就可以打开http://localhost:8000/admin
查看,登录Adding your models to the administration site
将
blog
中的模型添加到管理员站点,编辑admin.py
文件Customizing the way models are displayed
admin.py
The text was updated successfully, but these errors were encountered: