django 之model
在models.py中书写代码如下
修改了模型,先migrations 再执行migrate操作
sh-3.2# python manage.py syncdb
Unknown command: 'syncdb'
Type 'manage.py help' for usage.
sh-3.2# ls -l
total 32
drwxr-xr-x 6 wulili staff 204 10 29 10:27 .idea
-rwxrwxrwx 1 root staff 12288 10 28 15:01 db.sqlite3
-rwxrwxrwx 1 root staff 804 10 28 14:59 manage.py
drwxrwxrwx 2 root staff 68 10 28 20:46 media
drwxrwxrwx 10 root staff 340 10 29 09:22 mysite
drwxrwxrwx 13 root staff 442 10 29 10:27 order
drwxr-xr-x 2 wulili staff 68 10 28 20:56 templ
sh-3.2# manage.py help
sh: manage.py: command not found
sh-3.2# python manage.py help --也没找到syncdb
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
创建数据库和表结构
sh-3.2# python manage.py migrate 又是版本原因,我的 Django是1.10版本,要是用这个命令。
Operations to perform:
Apply all migrations: admin, auth, 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 sessions.0001_initial... OK
sh-3.2#
sh-3.2# python manage.py createsuperuser 创建管理员账号
Username (leave blank to use 'wulili'): root
Email address: [email protected]
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Password:
Password (again):
Superuser created successfully.
sh-3.2#
加了app后需要告诉django你的模型发生了改变,需要迁移数据库
sh-3.2# python manage.py makemigrations 迁移数据库
操作完成后一定记得停掉服务重新启动,pycharm中也要重新加载一下
在启动后,点击product报错,说没有order_product这个表,很是纳闷了,明明显示创建了,怎么还是报错呢
解决方法:把上面的几步重新执行一遍,就会好了。估计是重新加载了
sh-3.2# python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, order, sessions
Running migrations:
Applying order.0001_initial... OK
sh-3.2# python manage.py makemigrations
No changes detected
查看迁移的sql语句
sh-3.2# ls -l
total 24
-rw-r--r-- 1 root staff 660 10 29 14:09 0001_initial.py
-rw-r--r-- 1 wulili staff 948 10 29 14:12 0001_initial.pyc
-rwxrwxrwx 1 root staff 0 10 28 21:30 __init__.py
-rw-r--r-- 1 wulili staff 136 10 29 09:22 __init__.pyc
sh-3.2# pwd
/Users/wulili/mysite/order/migrations
sh-3.2# cd /Users/wulili/mysite/
sh-3.2#
sh-3.2# python manage.py sqlmigrate order 0001
BEGIN;
--
-- Create model Product
--
CREATE TABLE "order_product" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL, "price" real NOT NULL);
COMMIT;
sh-3.2#
添加字段后需要做如下操作
sh-3.2# python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, order, sessions
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
sh-3.2#
sh-3.2#
sh-3.2# python manage.py makemigrations
You are trying to add a non-nullable field 'Ptype' to product without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option: Ptype
Please select a valid option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
>>> exit
sh-3.2#
在修改了models.py的时候,还是报错,表找不到,真是够费劲,总是报这个错误
我就重新创建了数据库
删除之前创建的数据库
sh-3.2# ls -l
total 272
drwxr-xr-x 6 wulili staff 204 10 29 15:25 .idea
-rwxrwxrwx 1 root staff 135168 10 29 15:33 db.sqlite3
-rwxrwxrwx 1 root staff 804 10 28 14:59 manage.py
drwxrwxrwx 2 root staff 68 10 28 20:46 media
drwxrwxrwx 10 root staff 340 10 29 10:50 mysite
drwxrwxrwx 13 root staff 442 10 29 15:25 order
sh-3.2# rm -rf db.sqlite3
sh-3.2# ls -l
total 72
-rwxrwxrwx 1 root staff 0 10 28 21:30 __init__.py
-rwxrwxrwx 1 wulili staff 125 10 28 22:20 __init__.pyc
-rwxrwxrwx 1 wulili staff 154 10 29 15:25 admin.py
-rwxrwxrwx 1 wulili staff 312 10 29 15:25 admin.pyc
-rwxrwxrwx 1 root staff 126 10 28 21:30 apps.py
drwxrwxrwx 6 root staff 204 10 29 16:33 migrations
-rwxrwxrwx 1 wulili staff 491 10 29 15:07 models.py
-rwxrwxrwx 1 root staff 1197 10 29 15:09 models.pyc
-rwxrwxrwx 1 root staff 60 10 28 21:30 tests.py
-rwxrwxrwx 1 wulili staff 153 10 29 09:14 views.py
-rwxrwxrwx 1 wulili staff 407 10 29 09:21 views.pyc
sh-3.2# pwd
/Users/wulili/mysite/order
sh-3.2# rm -rf migrations/
重新创建
sh-3.2# python manage.py makemigrations
No changes detected
sh-3.2# python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, 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 sessions.0001_initial... OK
sh-3.2# cd order/
sh-3.2# ls -l
total 72
-rwxrwxrwx 1 root staff 0 10 28 21:30 __init__.py
-rwxrwxrwx 1 wulili staff 125 10 28 22:20 __init__.pyc
-rwxrwxrwx 1 wulili staff 154 10 29 15:25 admin.py
-rwxrwxrwx 1 wulili staff 312 10 29 15:25 admin.pyc
-rwxrwxrwx 1 root staff 126 10 28 21:30 apps.py
-rwxrwxrwx 1 wulili staff 491 10 29 15:07 models.py
-rwxrwxrwx 1 root staff 1197 10 29 15:09 models.pyc
-rwxrwxrwx 1 root staff 60 10 28 21:30 tests.py
-rwxrwxrwx 1 wulili staff 153 10 29 09:14 views.py
-rwxrwxrwx 1 wulili staff 407 10 29 09:21 views.pyc
sh-3.2# python manage.py makemigrations order
python: can't open file 'manage.py': [Errno 2] No such file or directory
sh-3.2# pwd
/Users/wulili/mysite/order
sh-3.2# cd ..
sh-3.2# python manage.py makemigrations order 这个命令会产生migrations这个目录
Migrations for 'order':
order/migrations/0001_initial.py:
- Create model Product
- Create model Ptype
- Add field Ptype to product
sh-3.2# ls -l
total 264
drwxrwxrwx 6 wulili staff 204 10 29 16:22 .idea
-rw-r--r-- 1 root staff 131072 10 29 16:25 db.sqlite3
-rwxrwxrwx 1 root staff 804 10 28 14:59 manage.py
drwxrwxrwx 2 root staff 68 10 28 20:46 media
drwxrwxrwx 10 root staff 340 10 29 10:50 mysite
drwxrwxrwx 13 root staff 442 10 29 16:29 order
drwxrwxrwx 2 wulili staff 68 10 28 20:56 templ
sh-3.2# cd order/
sh-3.2# ls -l
total 72
-rwxrwxrwx 1 root staff 0 10 28 21:30 __init__.py
-rwxrwxrwx 1 wulili staff 125 10 28 22:20 __init__.pyc
-rwxrwxrwx 1 wulili staff 154 10 29 15:25 admin.py
-rwxrwxrwx 1 wulili staff 312 10 29 15:25 admin.pyc
-rwxrwxrwx 1 root staff 126 10 28 21:30 apps.py
drwxr-xr-x 5 root staff 170 10 29 16:29 migrations
-rwxrwxrwx 1 wulili staff 491 10 29 15:07 models.py
-rwxrwxrwx 1 root staff 1197 10 29 15:09 models.pyc
-rwxrwxrwx 1 root staff 60 10 28 21:30 tests.py
-rwxrwxrwx 1 wulili staff 153 10 29 09:14 views.py
-rwxrwxrwx 1 wulili staff 407 10 29 09:21 views.pyc
sh-3.2# cd migrations/
sh-3.2# ls -l
total 16
-rw-r--r-- 1 root staff 1193 10 29 16:29 0001_initial.py
-rw-r--r-- 1 root staff 0 10 29 16:29 __init__.py
-rw-r--r-- 1 root staff 136 10 29 16:29 __init__.pyc
sh-3.2# chmod -R 777 /Users/wulili/mysite/
sh-3.2# python manage.py createsuperuser
python: can't open file 'manage.py': [Errno 2] No such file or directory
sh-3.2# cd ../..
sh-3.2# pwd
/Users/wulili/mysite
sh-3.2# python manage.py createsuperuser 创建个用户 从下面标红处可了解到,我应该在之前执行一下那个命令
You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): order.
Run 'python manage.py migrate' to apply them.
Username (leave blank to use 'wulili'): ^C
Operation cancelled.
sh-3.2# python manage.py sqlmigrate order 0001 查看数据库中数据是否对
BEGIN;
--
-- Create model Product
--
CREATE TABLE "order_product" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL, "price" real NOT NULL);
--
-- Create model Ptype
--
CREATE TABLE "order_ptype" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(10) NOT NULL);
--
-- Add field Ptype to product
--
ALTER TABLE "order_product" RENAME TO "order_product__old";
CREATE TABLE "order_product" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL, "price" real NOT NULL, "Ptype_id" integer NOT NULL REFERENCES "order_ptype" ("id"));
INSERT INTO "order_product" ("Ptype_id", "price", "id", "name") SELECT NULL, "price", "id", "name" FROM "order_product__old";
DROP TABLE "order_product__old";
CREATE INDEX "order_product_c54e703e" ON "order_product" ("Ptype_id");
COMMIT;
sh-3.2# python manage.py createsuperuser
You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): order.
Run 'python manage.py migrate' to apply them.
Username (leave blank to use 'wulili'): root
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
sh-3.2#
sh-3.2#
sh-3.2#
sh-3.2#
sh-3.2#
sh-3.2#
sh-3.2#
sh-3.2#
sh-3.2# python manage.py sqlmigrate order 0001
BEGIN;
--
-- Create model Product
--
CREATE TABLE "order_product" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL, "price" real NOT NULL);
--
-- Create model Ptype
--
CREATE TABLE "order_ptype" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(10) NOT NULL);
--
-- Add field Ptype to product
--
ALTER TABLE "order_product" RENAME TO "order_product__old";
CREATE TABLE "order_product" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL, "price" real NOT NULL, "Ptype_id" integer NOT NULL REFERENCES "order_ptype" ("id"));
INSERT INTO "order_product" ("Ptype_id", "price", "id", "name") SELECT NULL, "price", "id", "name" FROM "order_product__old";
DROP TABLE "order_product__old";
CREATE INDEX "order_product_c54e703e" ON "order_product" ("Ptype_id");
COMMIT;
sh-3.2# ls -l
total 264
drwxrwxrwx 6 wulili staff 204 10 29 16:44 .idea
-rwxrwxrwx 1 root staff 131072 10 29 16:41 db.sqlite3
-rwxrwxrwx 1 root staff 804 10 28 14:59 manage.py
drwxrwxrwx 2 root staff 68 10 28 20:46 media
drwxrwxrwx 10 root staff 340 10 29 10:50 mysite
drwxrwxrwx 13 root staff 442 10 29 16:29 order
drwxrwxrwx 2 wulili staff 68 10 28 20:56 templ
sh-3.2# cd order/
sh-3.2# ls -l
total 72
-rwxrwxrwx 1 root staff 0 10 28 21:30 __init__.py
-rwxrwxrwx 1 wulili staff 125 10 28 22:20 __init__.pyc
-rwxrwxrwx 1 wulili staff 154 10 29 15:25 admin.py
-rwxrwxrwx 1 wulili staff 312 10 29 15:25 admin.pyc
-rwxrwxrwx 1 root staff 126 10 28 21:30 apps.py
drwxrwxrwx 6 root staff 204 10 29 16:33 migrations
-rwxrwxrwx 1 wulili staff 491 10 29 15:07 models.py
-rwxrwxrwx 1 root staff 1197 10 29 15:09 models.pyc
-rwxrwxrwx 1 root staff 60 10 28 21:30 tests.py
-rwxrwxrwx 1 wulili staff 153 10 29 09:14 views.py
-rwxrwxrwx 1 wulili staff 407 10 29 09:21 views.pyc
sh-3.2# python manage.py migrate
python: can't open file 'manage.py': [Errno 2] No such file or directory
sh-3.2# cd ..
sh-3.2# ls -l
total 264
drwxrwxrwx 6 wulili staff 204 10 29 16:44 .idea
-rwxrwxrwx 1 root staff 131072 10 29 16:41 db.sqlite3
-rwxrwxrwx 1 root staff 804 10 28 14:59 manage.py
drwxrwxrwx 2 root staff 68 10 28 20:46 media
drwxrwxrwx 10 root staff 340 10 29 10:50 mysite
drwxrwxrwx 13 root staff 442 10 29 16:29 order
drwxrwxrwx 2 wulili staff 68 10 28 20:56 templ
sh-3.2# python manage.py migrate 这里执行一下就好了,重新登录,终于不报表不存在的错误了
Operations to perform:
Apply all migrations: admin, auth, contenttypes, order, sessions
Running migrations:
Applying order.0001_initial... OK
sh-3.2# pwd
/Users/wulili/mysite
本文转自aaa超超aaa 51CTO博客,原文链接:http://blog.51cto.com/10983441/1867048