1. 程式人生 > >python3.5下django2.0使用pymysql連接mysql

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

django pymysql

創建數據庫

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


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