默认情况下,Django的数据库是【SQLite3】。

如下:
在具体的Django的App的目录中的文件【settings.py】中定义:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

要改变Django的数据库设置,有这几方面的配置需要修改:
1. settings.py,添加数据库连接的信息
2. __init__.py,导入Python操纵MySQL的模块【mysqlclient / pymysql】
3. MySQL:准备好数据库

MySQL:
创建数据库


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database django_me;
Query OK, 1 row affected (0.01 sec)

mysql>
mysql> create user django_me@'%' identified by '********';
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> grant all privileges on django_me.* to 'django_me'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql>

可以看到,这时候创建的只是个空库。(现阶段,这样就够了。)

Django项目,Application目录中:
文件【__init__.py】,导入Python的MySQL模块:

# import

import pymysql

# do - 1

# modules: pymysql
pymysql.install_as_MySQLdb()

文件【settings.py】
其中,【DATABASES】的部分

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

为了防止下一步【初始化数据库】出现问题:
1. mysqlclient
2. python 3.x
3. 其他的问题

需要对以下目录做出修改:
【项目目录】下的【venv\Lib\site-packages\django\db\backends\mysql】
1. base.py:【http://d-prototype.com/archives/13557】
2. operations.py:【http://d-prototype.com/archives/13565】

Django:manage.py,初始化数据库:
1. 1.9以前:manage.py syncdb
2. 1.9以后:
—-> manage.py migrate
—-> manage.py makemigrations

我的Django为2.2的版本,因此我用【migrate】的方法初始化数据库(MySQL):

PS D:\PyCharm_data\django_me> .\venv\Scripts\python.exe manage.py help

Type 'manage.py help ' for help on a specific subcommand.

Available subcommands:

[auth]
    changepassword
    createsuperuser

[contenttypes]
    remove_stale_contenttypes

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver

[sessions]
    clearsessions

[staticfiles]
    collectstatic
    findstatic
    runserver
PS D:\PyCharm_data\django_me>

PS D:\PyCharm_data\django_me> .\venv\Scripts\python.exe manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, 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 auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK
PS D:\PyCharm_data\django_me>

初始化成功后,你会看到MySQL数据库中,就已经有数据了:

这样,Django的数据库就已经更换成功了。

后面,运行Django就好了:

PS D:\PyCharm_data\django_me> .\venv\Scripts\python.exe manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
April 09, 2019 - 13:40:01
Django version 2.2, using settings 'django_me.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[09/Apr/2019 13:41:53] "GET / HTTP/1.1" 200 16348
[09/Apr/2019 13:41:53] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0
[09/Apr/2019 13:41:54] "GET / HTTP/1.1" 200 16348
[09/Apr/2019 13:41:54] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0

——————————————
Done。

说点什么

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒