Django中更改默認數(shù)據(jù)庫為mysql的方法示例
Django中默認使用sqlite3數(shù)據(jù)庫,今天研究了下如何將它換成常見的mysql數(shù)據(jù)庫。
由于項目用得python3,而MySQLdb沒有支持python3的版本,如果使用python3.x版本時,pip install MySQLdb時會報錯。
后來通過谷歌發(fā)現(xiàn)可以使用pymysql替代MySQLdb
1 在項目根目錄下的__init__.py文件中加入如下代碼:
import pymysql pymysql.install_as_MySQLdb()
2 使用mysqlclient代替MySQLdb,安裝方式為:
pip install mysqlclient
3 更改項目setting.py中對數(shù)據(jù)庫的配置為:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'test', 'USER': 'username', 'PASSWORD': 'passwd', 'HOST': 'localhost', 'PORT': '3306' } }
4 最后通過python manage.py migrate命令,Django會在數(shù)據(jù)庫中自動創(chuàng)建相應(yīng)的表
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 在創(chuàng)建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來執(zhí)行命令,切換到Windows自帶的命令行,可以解決該問題!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python利用pynimate實現(xiàn)制作動態(tài)排序圖
這篇文章主要為大家詳細介紹了Python如何利用pynimate實現(xiàn)制作動態(tài)排序圖,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-02-02python3調(diào)用windows dos命令的例子
今天小編就為大家分享一篇python3調(diào)用windows dos命令的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08OpenCV實現(xiàn)手勢虛擬拖拽的使用示例(附demo)
本文主要介紹了OpenCV實現(xiàn)手勢虛擬拖拽的使用示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-11-11Python multiprocessing.Manager介紹和實例(進程間共享數(shù)據(jù))
這篇文章主要介紹了Python multiprocessing.Manager介紹和實例(進程間共享數(shù)據(jù)),本文介紹了Manager的dict、list使用例子,同時介紹了namespace對象,需要的朋友可以參考下2014-11-11python入門課程第四講之內(nèi)置數(shù)據(jù)類型有哪些
這篇文章主要介紹了python入門課程第四講之內(nèi)置數(shù)據(jù)類型有哪些?本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09