django2.1新手教程
http://HdhCmsTestliujiangblog测试数据/blog/36/
数据库同步操作
https://HdhCmsTestjianshu测试数据/p/22aa7cca7ff6
自动生成sql语句django会根据setting.py中指定的数据库自动生成sql语句:
python manage.py makemigrations查看自动生成的sql语句
python manage.py sqlmigrate 【appname】 【no】 例:python manage.py sqlmigrate myblog 0001自动同步到数据库
python manage.py migratebug: MySQL Strict Mode
WARNINGS: ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default' HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended y ou activate it. See: https://docs.djangoproject测试数据/en/2.1/ref/databases/#mysql-sql-mode解决方法1
在settings中,在DATABASES变量定义处下面添加
DATABASES['OPTIONS']['init_command'] = "SET sql_mode='STRICT_TRANS_TABLES'"解决方法2
1 2 3 4 5 6 7 8 9 10 11 12 13
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'cluster', 'USER':'root', 'PASSWORD':'', 'HOST':'localhost', 'PORT':'3306', 'OPTIONS':{ 'init_command':"SET sql_mode='STRICT_TRANS_TABLES'", } } }bug: 无法自动生成表
Running migrations: No migrations to apply.
solution 第一步: 删除该app名字下的migrations文件(migration文件夹中的000x-initial.py文件,存储了自动生成的sql创表语句)。 第二步: 进入数据库,找到django_migrations的表,删除表中app字段为该app名字的所有记录。 第三步: pycharm的Terminal中执行python manage.py makemigrations python manage.py migrate bug:No module named ‘MySQLdb’ solution1用pymysql代替。在项目文件夹下的init.py(settings.py也可以?)添加如下代码即可
1 2
import pymysql pymysql.install_as_MySQLdb()solution2 step1: 安装mysqlclient, MySQLdb的分叉版本,加入了对python3的支持。mysqlclient下载地址:https://HdhCmsTestlfd.uci.edu/~gohlke/pythonlibs/ step2: 下载该文件后在下载文件所在目录运行cmd(或运行cmd后切换到该文件目录下),执行 pip install mysqlclient
原文:大专栏 django与MySQL
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did171001