python3.5下django2.0使用pymysql连接mysql

创建数据库

create database haha default charset=utf8;

安装pymysql

pip install PyMySQL

应用中的setting.py设置如下

import pymysql
pymysql.install_as_MySQLdb()
DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'mysite',
    'USER': 'root',
    'PASSWORD': '123456',
    'HOST': '127.0.0.1',
    'PORT': '3306',
    }
}

在C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\mysql\base.py把下面的内容注释掉

if version < (1, 3, 3):
     raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)




创建模型

from django.db import models

class Post(models.Model):
    title = models.CharField('标题', max_length=70)
    
    def __str__(self):
        return self.title

迁移模型

python manage.py makemigrations
python manage.py migrate


文章来自:http://blog.51cto.com/wenguonideshou/2047424
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3