django setting 配置数据库
setting.py
1、sqlite3
# 需要下载sqlite3库
pip install sqlite3
# setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
2、mysql
# 需要下载pymysql库
pip install pymysql
# setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': '3306'
}
}
3、oracle
# 需要下载cx_Oracle库
pip install cx_Oracle
# setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'db_name',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': '1521'
}
}
4、postgrasql
# 需要下载psycopg2库
pip install psycopg2
# setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db_name',
'USER': 'root',
'PASSWORD': '123456',
'HOST': 'localhost',
'PORT': '5432',
}
}
# 设置了DATABASES 且创建了model后,在terminal执行迁移数据库,
python manage.py makemigrations
python manage.py migrate