django下数据库配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django下数据库配置相关的知识,希望对你有一定的参考价值。

1、django默认支持sqlite、mysql、Oracle、postgresql数据库,像db2和sqlserver之类的数据库之类的数据库支持需要第三方的支持。具体详见:
https://docs.djangoproject.com/en/1.9/ref/databases/

Using a 3rd-party database backend?

In addition to the officially supported databases, there are backends provided by 3rd parties that allow you to use other databases with Django:

SAP SQL Anywhere
IBM DB2
Microsoft SQL Server
Firebird
ODBC
The Django versions and ORM features supported by these unofficial backends vary considerably. Queries regarding the specific capabilities of these unofficial backends, along with any support queries, should be directed to the support channels provided by each 3rd party project.

sqlite:
django默认使用sqlite的数据库,默认自带sqlite的数据库驱动,
引擎名称:
django.db.backends.sqlite3

mysql
引擎名称:
django.db.backends.mysql

2、MySQL驱动引擎下载地址
MySQLdb(mysql-python):https://pypi.python.org/pypi/MySQL-python/1.2.5,仅仅支持Python2,Python2以后就没有更新,所以不支持Python3
mysqlclient:https://pypi.python.org/pypi/mysqlclient,是mysqldb的一个分支,支持Python3

MySQL官方驱动
Connectoer/Python: https://dev.mysql.com/downloads/connectoter/python
针对各个版本的Python都有特定的支持包

PyMySQL(纯python的mysql驱动):
http://pypyi.python.org/pypi/PyMySQL
本次课程的Python驱动采用该驱动支持

3、演示pymysql的使用
1、安装pymysql
2、修改配置文件

错误提示:
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named ‘MySQLdb‘

django用mysql数据库时,默认数据库引擎时导入mysqldb模块,所以为了不出现修改默认引擎,可以做如下操作:
import pymysql
pymysql.install_as_MySQLdb()

将上述代码加入到settings.py中的即可,我这里直接放在DATABASES上:
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
import pymysql
pymysql.install_as_MySQLdb()
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘NAME‘: ‘hello_django_db‘,
‘USER‘:‘root‘,
‘PASSWORD‘:‘123456‘,
‘HOST‘:‘127.0.0.1‘,
‘PORT‘:‘3306‘,
}
}

接下来做数据库同步
Tools--->Run manage.py Task

makemigrations--->生成数据库脚本文件
migrate------>生成数据库表和数据

[email protected]_django > makemigrations
D:\JetBrains\bin\runnerw.exe C:\Users\Administrator\Documents\Workspace\venv\django_basic_venv\Scripts\python.exe D:\JetBrains\helpers\pycharm\django_manage.py makemigrations C:/Users/Administrator/Documents/Workspace/pycharm/hello_django
No changes detected
Following files were affected
Process finished with exit code 0
[email protected]_django > migrate
D:\JetBrains\bin\runnerw.exe C:\Users\Administrator\Documents\Workspace\venv\django_basic_venv\Scripts\python.exe D:\JetBrains\helpers\pycharm\django_manage.py migrate C:/Users/Administrator/Documents/Workspace/pycharm/hello_django
Operations to perform:
Apply all migrations: contenttypes, admin, sessions, auth
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying sessions.0001_initial... OK
Following files were affected
Process finished with exit code 0

以上是关于django下数据库配置的主要内容,如果未能解决你的问题,请参考以下文章

Django:Python3.6.2+Django2.0配置MySQL

Django---MTV模型基本命令简单配置

Django基于ORM数据库配置

Django配置Mysql数据库 (Pycharm)

Django配置MySQL数据库教程

Django配置MySQL数据库教程