Python 相关操作及一般问题
1:问题
TypeError: __init__() missing 1 required positional argument: 'on_delete'
#添加后问题解决 添加 on_delete class Car(models.Model): manufacturer = models.ForeignKey( 'Manufacturer', on_delete=models.CASCADE, )
2:相关操作
migrate: 用于执行迁移动作
makemigrations: 基于当前的model创建新的迁移策略文件
sqlmigrate: 显示迁移的SQL语句
(1)migrations的使用非常简单: 修改model, 比如增加field, 然后运行
python manager.py makemigrations
(2)你的mmodel会被扫描, 然后与之前的版本作比较, 在app的migrations目录下生成本次迁移文件.我们建议查看一下该迁移文件, 确保没有问题. 然后运行:
python manager.py migrate
3:重建数据库
(1)在Termianl 终端查找
ls -l
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
(2)删除
rm -rf db.sqlite3
(3)查看是否删除了
ls -l
-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
(4)删除migrations
pwd
/Users/Dotokp/mysite/order
rm -rf migrations/
(5) 重新创建
python manage.py makemigrations
python manage.py migrate
有时候会报错:
python: can't open file 'manage.py': [Errno 2] No such file or directory
cd ..
python manage.py makemigrations order 这个命令会产生migrations这个目录
ls -l
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
cd order/
ls -l
-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