Django中更改默认数据库为mysql的方法示例


Posted in Python onDecember 05, 2018

Django中默认使用sqlite3数据库,今天研究了下如何将它换成常见的mysql数据库。

由于项目用得python3,而MySQLdb没有支持python3的版本,如果使用python3.x版本时,pip install MySQLdb时会报错。

后来通过谷歌发现可以使用pymysql替代MySQLdb

1 在项目根目录下的__init__.py文件中加入如下代码:

import pymysql
pymysql.install_as_MySQLdb()

2 使用mysqlclient代替MySQLdb,安装方式为:

pip install mysqlclient

3 更改项目setting.py中对数据库的配置为:

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'test',
    'USER': 'username',
    'PASSWORD': 'passwd',
    'HOST': 'localhost',
    'PORT': '3306'
  }
}

4 最后通过python manage.py migrate命令,Django会在数据库中自动创建相应的表

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... 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 auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying polls.0001_initial... OK
  Applying sessions.0001_initial... OK

5 在创建admin用户时,遇到了如下报错

python manage.py createsuperuser
Superuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.

后来查了一下,是因为使用了git来执行命令,切换到Windows自带的命令行,可以解决该问题!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
高性能web服务器框架Tornado简单实现restful接口及开发实例
Jul 16 Python
在Python的循环体中使用else语句的方法
Mar 30 Python
在Python的Django框架中实现Hacker News的一些功能
Apr 17 Python
Python中处理字符串之islower()方法的使用简介
May 19 Python
Python中集合的内建函数和内建方法学习教程
Aug 19 Python
如何在Python函数执行前后增加额外的行为
Oct 20 Python
Python制作简易注册登录系统
Dec 15 Python
Python实现识别手写数字 Python图片读入与处理
Mar 23 Python
python3实现字符串操作的实例代码
Apr 16 Python
pip安装python库的方法总结
Aug 02 Python
python实现密码验证合格程序的思路详解
Jun 01 Python
Django执行源生mysql语句实现过程解析
Nov 12 Python
Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】
Dec 05 #Python
在python2.7中用numpy.reshape 对图像进行切割的方法
Dec 05 #Python
Python3爬虫爬取百姓网列表并保存为json功能示例【基于request、lxml和json模块】
Dec 05 #Python
python3 实现对图片进行局部切割的方法
Dec 05 #Python
python中强大的format函数实例详解
Dec 05 #Python
python3+opencv3识别图片中的物体并截取的方法
Dec 05 #Python
解决Python3.5+OpenCV3.2读取图像的问题
Dec 05 #Python
You might like
调试一段PHP程序时遇到的三个问题
2012/01/17 PHP
php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
2016/05/28 PHP
window.js 主要包含了页面的一些操作
2009/12/23 Javascript
js中将HTMLCollection/NodeList/伪数组转换成数组的代码
2011/07/31 Javascript
Jquery通过JSON字符串创建JSON对象
2014/08/24 Javascript
jQuery实现Twitter的自动文字补齐特效
2014/11/28 Javascript
js获取页面description的方法
2015/05/21 Javascript
Angular.js如何从PHP读取后台数据
2016/03/24 Javascript
使用PHP+JavaScript将HTML页面转换为图片的实例分享
2016/04/18 Javascript
解决jQuery ajax请求在IE6中莫名中断的问题
2016/06/20 Javascript
jQuery基础_入门必看知识点
2016/07/04 Javascript
详解webpack 多入口配置
2017/06/16 Javascript
简单实现jquery隔行变色
2017/11/09 jQuery
vue中路由参数传递可能会遇到的坑
2017/12/07 Javascript
jquery的 filter()方法使用教程
2018/03/22 jQuery
node.js部署之启动后台运行forever的方法
2018/05/23 Javascript
Vue侦测相关api的实现方法
2019/05/22 Javascript
微信小程序用canvas画图并分享
2020/03/09 Javascript
Python中用memcached来减少数据库查询次数的教程
2015/04/07 Python
介绍Python的Django框架中的静态资源管理器django-pipeline
2015/04/25 Python
浅谈Python 集合(set)类型的操作——并交差
2016/06/30 Python
利用Python脚本生成sitemap.xml的实现方法
2017/01/31 Python
python 表达式和语句及for、while循环练习实例
2017/07/07 Python
Python3编程实现获取阿里云ECS实例及监控的方法
2017/08/18 Python
Python获取系统所有进程PID及进程名称的方法示例
2018/05/24 Python
python中从for循环延申到推导式的具体使用
2019/11/29 Python
Python通用唯一标识符uuid模块使用案例
2020/09/10 Python
Python+OpenCV图像处理——实现轮廓发现
2020/10/23 Python
新西兰最大的品牌运动鞋购物网站:Platypus NZ
2017/10/27 全球购物
美国球鞋寄卖网站:Stadium Goods
2018/05/09 全球购物
PatPat阿根廷:妈妈们的购物平台
2019/05/30 全球购物
关于母亲节的感言
2014/02/04 职场文书
小学语文教研活动总结
2014/07/01 职场文书
基层党支部承诺书
2015/04/30 职场文书
领导干部学习三严三实心得体会
2016/01/05 职场文书
MongoDB误操作后使用oplog恢复数据
2022/04/11 MongoDB