Django3 數據庫安裝

settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'py',  # 數據庫名
        'HOST': '127.0.0.1', # 數據庫服務器地址
        'USER': 'root',  # 用戶名
        'PASSWORD':'19970223',# 密碼
        'PORT':3306      # 端口
    }
}

App.models

from django.db import models

# Create your models here.
# 自定義模型必須繼承Model
class User(models.Model):
    # 字段名:不能是python的關鍵字;不能使用連續的下劃線;
    #db_column:數據庫表中的字段名稱
    uid = models.AutoField(primary_key=True)
    # CharField類型必須指明長度max_length
    username = models.CharField(max_length=30,unique=True)
    password = models.CharField(max_length=128)
    regtime = models.DateTimeField(auto_now_add=True)

    class Meta: # 元數據,模型本身的信息
        #默認表名:應用名_模型名
        db_table = 'user'  # 表名

mysqlclient(不能使用!)

pip install mysqlclient
python manage.py makemigrations
python manage.py migrate

pymysql

init

import pymysql
pymysql.install_as_MySQLdb()

base

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

operations

if query is not None:
    query = query.encode(errors='replace')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章