Django 与 MySQL 对接

Django 与 MySQL 对接

环境和目标

mysql直接安装在Mac

djangovirtualenv中运行

已经在virtualenv下安装django,并使用sqlite3搭建过简单的网站;单独使用过mysql,这次的目的是进行virtualenv下的django与系统环境下的MySQL对接。

步骤

在 MySQL 中创建新数据库 和 账号,并授权

MySQL创建一个新数据库web

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

设置为当前使用的数据库

mysql> use web
Database changed
mysql> show tables;
Empty set (0.00 sec)

创建django访问web数据库的账号,并在数据库mysql的表user中检查

mysql> create user webproject@localhost identified by 'web';
ERROR 1396 (HY000): Operation CREATE USER failed for 'webproject'@'localhost'

mysql> create user 'webproject@localhost' identified by 'web';
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| menagerie          |
| mysql              |
| performance_schema |
| sys                |
| test               |
| web                |
+--------------------+
7 rows in set (0.00 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.00 sec)

mysql> select * from user where User='webproject';
+-----------+------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| Host      | User       | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin                | authentication_string                     | password_expired | password_last_changed | password_lifetime | account_locked |
+-----------+------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| localhost | webproject | N           | N           | N           | N           | N           | N         | N           | N             | N            | N         | N          | N               | N          | N          | N            | N          | N                     | N                | N            | N               | N                | N                | N              | N                   | N                  | N                | N          | N            | N                      |          |            |             |              |             0 |           0 |               0 |                    0 | mysql_native_password | *7F3BF7031A324F9FA79930B44A098C84FA3FBB97 | N                | 2015-11-28 10:31:31   |              NULL | N              |
+-----------+------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
1 row in set (0.00 sec)

切回数据库web

mysql> use web
Database changed

webproject@localhost开放访问数据库web中所有表的所有权限

mysql> grant all privileges on web.* to webproject@localhost;
Query OK, 0 rows affected (0.00 sec)

刷新权限

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

创建新 django 项目,设置参数,进行对接

进入virtualenv环境

➜  django  source bin/activate

创建项目web

➜  project  django-admin startproject web
➜  project  ls
web
➜  project  web
➜  web  ls
manage.py web

修改settings.py文件:和数据库对接的参数、时区

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

# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'

或者这样

# settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': '/path/to/my.cnf',
        },
    }
}


# my.cnf
[client]
database = web
user = webproject
password = web
default-character-set = utf8

使用Git进行版本管理

➜  web  git init
➜  web git:(master) ✗ git add .
➜  web git:(master) ✗ git commit -m 'init django site'

生成迁移策略文件

➜  web git:(master) ✗ python manage.py makemigrations
Traceback (most recent call last):

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

MySQL 有三个接口程序,官网资料

这里按报错,是因为还没有安装与 MySQL 对接的接口程序,官方的接口程序中,前两个都在PyPI中发布了,可以通过 pip 进行安装,第三个没试过。

先按照提示用 mysqldb。

➜  web git:(master) ✗ pip install MysqL-python
Collecting MysqL-python
  Downloading MySQL-python-1.2.5.zip (108kB)
    100% |████████████████████████████████| 110kB 8.3kB/s
    Complete output from command python setup.py egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/private/var/folders/36/tdbs35wx1sx4d6nm4hrmwy0h0000gn/T/pip-build-mIngAC/MysqL-python/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/36/tdbs35wx1sx4d6nm4hrmwy0h0000gn/T/pip-build-mIngAC/MysqL-python

又报错,找不到mysql_config

向 环境变量添加mysql_config的路径

PATH=$PATH:/usr/local/mysql/bin/:

设置为全局变量

export PATH

再次安装MysqL-python

➜  web git:(master) ✗ pip install MysqL-python
Collecting MysqL-python
  Using cached MySQL-python-1.2.5.zip
Building wheels for collected packages: MysqL-python
  Running setup.py bdist_wheel for MysqL-python
  Stored in directory: /Users/chao/Library/Caches/pip/wheels/8c/0d/11/d654cad764b92636ce047897dd2b9e1b0cd76c22f813c5851a
Successfully built MysqL-python
Installing collected packages: MysqL-python
Successfully installed MysqL-python-1.2.5

或者,可以安装mysqlclient

➜  web git:(master) ✗ pip install mysqlclient
Collecting mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.7

再次生成策略文件

➜  web git:(master) ✗ python manage.py makemigrations
No changes detected

执行策略文件中的策略

➜  web git:(master) ✗ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... 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 sessions.0001_initial... OK

表 生成成功。django会自动创建一些自身需要的表。

mysql> show tables;
+----------------------------+
| Tables_in_web              |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
10 rows in set (0.00 sec)

创建站点管理员账号,运行服务器,登陆站点后台

创建一个django的管理员账号

➜  web git:(master) ✗ python manage.py createsuperuser
Username (leave blank to use 'chao'): admin
Email address: admin@example.com
Password:
Password (again):
Superuser created successfully.

可以看到 中添加了对应的一条

mysql> select * from auth_user;
+----+-------------------------------------------------------------------------------+------------+--------------+----------+------------+-----------+-------------------+----------+-----------+----------------------------+
| id | password                                                                      | last_login | is_superuser | username | first_name | last_name | email             | is_staff | is_active | date_joined                |
+----+-------------------------------------------------------------------------------+------------+--------------+----------+------------+-----------+-------------------+----------+-----------+----------------------------+
|  1 | pbkdf2_sha256$20000$Hr0nUzl4tsNy$SKvpb5EzjyIOGldrtb07xQpKa5VY9/OPw50q2neRMT0= | NULL       |            1 | admin    |            |           | admin@example.com |        1 |         1 | 2015-11-28 03:36:03.513564 |
+----+-------------------------------------------------------------------------------+------------+--------------+----------+------------+-----------+-------------------+----------+-----------+----------------------------+
1 row in set (0.00 sec)

运行服务器

➜  web git:(master) ✗ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
November 28, 2015 - 11:58:35
Django version 1.8.6, using settings 'web.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[28/Nov/2015 11:58:50] "GET / HTTP/1.1" 200 1767

在浏览器中通过http://127.0.0.1:8000/访问,可以看到信息

![Uploading 屏幕快照 2015-11-28 下午12.00.40_322750.png . . .]](http://upload-images.jianshu.io/upload_images/413992-09720f35b6a594c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

登陆管理后台

http://127.0.0.1:8000/admin/

输入创建的用户名和密码,登陆

屏幕快照 2015-11-28 下午12.02.45.png

对数据库进行操作

ORM

  • 创建一个条目

插入一条,并写入数据库

>>> from blog.models import Blog
>>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')
>>> b.save()

或者

create(**kwargs)

p = Person.objects.create(first_name="Bruce", last_name="Springsteen")

  • 修改、保存
>>> b5.name = 'New name'
>>> b5.save()
  • 增加多个条目
>>> john = Author.objects.create(name="John")
>>> paul = Author.objects.create(name="Paul")
>>> george = Author.objects.create(name="George")
>>> ringo = Author.objects.create(name="Ringo")

>>> entry.authors.add(john, paul, george, ringo)

  • all()

all_entries = Entry.objects.all()

  • filter(**kwargs)

Entry.objects.filter(pub_date__year=2006)

等同于

SELECT * FROM blog_entry WHERE pub_date__year = '2006';

  • exclude(**kwargs)

  • get(**kwargs),获取单个目标

Entry.objects.get(pk=1)

  • 分片

Entry.objects.all()[:5],返回头五个对象,(LIMIT 5)

Entry.objects.all()[5:10],偏移五,获取五个

  • order_by

按某一列的字母顺序排列

>>> Entry.objects.order_by('headline')[0]
  • get_or_create(defaults=None, **kwargs)

obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)})

  • update_or_create(defaults=None, **kwargs)

  • count()

Entry.objects.count()

等同于

SELECT COUNT(*)

  • delete()
>>> blogs = Blog.objects.all()

# 删除所有的博客和条目对象
>>> blogs.delete()
  • save()

ORM 查询

查询

SQL 语句

创建模型

class Person(models.Model):
    first_name = models.CharField(...)
    last_name = models.CharField(...)
    birth_date = models.DateField(...)
  • raw(raw_query, params=None, translations=None)

Person.objects.raw('SELECT * FROM myapp_person')

等同于

Person.objects.all()

first_person = Person.objects.raw('SELECT * FROM myapp_person')[0] 索引

  • 直接执行自定义的 SQL
from django.db import connection

def my_custom_sql(self):
    cursor = connection.cursor()

    cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])

    cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
    row = cursor.fetchone()

    return row

raw

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343

推荐阅读更多精彩内容