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 相关文章推荐
Python的Urllib库的基本使用教程
Apr 30 Python
python动态性强类型用法实例
May 09 Python
Python的Django框架中settings文件的部署建议
May 30 Python
Go语言基于Socket编写服务器端与客户端通信的实例
Feb 19 Python
python使用turtle绘制分形树
Jun 22 Python
详解PyCharm配置Anaconda的艰难心路历程
Aug 13 Python
pandas通过loc生成新的列方法
Nov 28 Python
python3.6使用urllib完成下载的实例
Dec 19 Python
Python递归实现打印多重列表代码
Feb 27 Python
解决keras,val_categorical_accuracy:,0.0000e+00问题
Jul 02 Python
python解释器安装教程的方法步骤
Jul 02 Python
Tensorflow使用Anaconda、pycharm安装记录
Jul 29 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异步执行的详解
2013/06/03 PHP
解析php利用正则表达式解决采集内容排版的问题
2013/06/20 PHP
PHP生成指定随机字符串的简单实现方法
2015/04/01 PHP
PHP实现的简单缓存类
2015/07/29 PHP
Yii CFileCache 获取不到值的原因分析
2017/02/08 PHP
微信小程序 消息推送php服务器验证实例详解
2017/03/30 PHP
php实现文件上传基本验证
2020/03/04 PHP
HTTP头隐藏PHP版本号实现过程解析
2020/12/09 PHP
javascript 解析后的xml对象的读取方法细解
2009/07/25 Javascript
javascript自执行函数之伪命名空间封装法
2010/12/25 Javascript
浅谈javascript中的作用域
2012/04/07 Javascript
jquery UI Datepicker时间控件的使用方法(基础版)
2015/11/07 Javascript
jquery实现手风琴效果
2015/11/20 Javascript
jQuery1.9+中删除了live以后的替代方法
2016/06/17 Javascript
js从数组中删除指定值(不是指定位置)的元素实现代码
2016/09/13 Javascript
谈谈Vue.js——vue-resource全攻略
2017/01/16 Javascript
bootstrap multiselect 多选功能实现方法
2017/06/05 Javascript
JS实现带导航城市列表以及输入搜索功能
2018/01/04 Javascript
详解vue2.0 资源文件assets和static的区别
2018/11/27 Javascript
详解mpvue实现对苹果X安全区域的适配
2019/07/31 Javascript
Vue.js 实现地址管理页面思路详解(地址添加、编辑、删除和设置默认地址)
2019/12/11 Javascript
JS 设计模式之:工厂模式定义与实现方法浅析
2020/05/06 Javascript
vue自定义组件(通过Vue.use()来使用)即install的用法说明
2020/08/11 Javascript
使用python遍历指定城市的一周气温
2017/03/31 Python
Python3.5集合及其常见运算实例详解
2019/05/01 Python
Python 继承,重写,super()调用父类方法操作示例
2019/09/29 Python
基于python实现监听Rabbitmq系统日志代码示例
2020/11/28 Python
goodhealth官方海外旗舰店:新西兰国民营养师
2017/12/15 全球购物
大学新生军训感言
2014/02/25 职场文书
绿色环保标语
2014/06/12 职场文书
员工年终自我评价
2014/09/14 职场文书
2014广电局实施党的群众路线教育实践活动方案思想汇报
2014/09/22 职场文书
2014年药剂科工作总结
2014/11/26 职场文书
放假通知范文
2015/04/14 职场文书
Redis持久化与主从复制的实践
2021/04/27 Redis
Win10多屏显示如何设置?Win10电脑多屏显示设置操作方法
2022/07/07 数码科技