mysql 鏈接 django

版本:html

django:1.11.9python

python3mysql

mysql 5.7.18sql

在這裏咱們認爲你已經安裝好了mysql,python ,django數據庫

下面是來自django官方教程的一段話django

If you wish to use another database, install the appropriate database bindings session

and change the following keys in theDATABASES 'default' item to match your database connection settings:app

first

你須要一個Python的db API DRIVER   即數據庫接口驅動ide

常見的有mysqldb、pymysql、mysqlclient  ui

  • MySQLdb is a native driver that has been developed and supported for over a decade by Andy Dustman.
  • mysqlclient is a fork of MySQLdb which notably supports Python 3 and can be used as a drop-in replacement for MySQLdb. At the time of this writing, this is the recommended choice for using MySQL with Django.
  • MySQL Connector/Python is a pure Python driver from Oracle that does not require the MySQL client library or any Python modules outside the standard library.

可是MYSQLdb不支持python3  ,

In addition to a DB API driver, Django needs an adapter to access the database drivers from its ORM.

Django provides an adapter for MySQLdb/mysqlclient while MySQL Connector/Python includes its own.

上面的意思是不只須要接口驅動,還須要適配器,django已經爲MYSQLdb和mysqlclient提供了適配器,MySQL Connector/Python內置這個適配器

雖然我有一個pymysql ,但按照官網的推薦我仍是下載了一個mysqlclient

>>pip install mysqlclient

建立一個數據庫

打開mysql,type:

CREATE  DATABASE  database_name CHARACTER SET UTF8;       指定數據庫的編碼utf8

Now ,咱們已經有了db API DRIVER 和建立好的數據庫,開始第二步

second

改變配置文件

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite',
        'HOST':'',
        'PORT':'3306',
        'USER':'root',
        'PASSWORD':'123',
    }
}

NAME   是你建立的數據庫名字

HOST  主機

PORT 端口

USER  用戶名    這個用戶的權限應是能建立數據表的或者數據庫,忘了。。

PASSWORD  登陸mysql的密碼

還有其餘選項

third  

如今咱們能夠建立一些模型去加到數據庫裏了

例如像下面

from django.db import models

# Create your models here.

class Publisher(models.Model):
    pub_name=models.CharField(max_length=200)
    city=models.CharField(max_length=200)

class Book(models.Model):
    book_name = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    publish = models.ForeignKey(Publisher, on_delete=models.CASCADE)
View Code

calss  定義的類名將會是數據庫對應的表名,屬性對應字段,不過代表有所誤差,假如你的應用名是blog ,class名是Book, 建立的表則是blog_book   數據庫不區分大小寫

note:

在配置文件settings.py 修改

INSTALLED_APPS = [
    'blog.apps.BlogConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
View Code

將咱們寫的應用添加進去,

'blog.apps.BlogConfig', 紅色部分是咱們應用app.py文件中的類名。

last

 

切換到項目的目錄下

like

 

執行如下幾條命令

python manage.py makemigrations

以上是提示錯誤,表示在Publisher 這個類的屬性不能是book,因此起名字要注意

改完字段以後

OK

你可使用

Python manage.py sqlmigrate 應用名 0001

去查看下,這條命令不會作任何操做,只是查看

python manage.py migrate

建立完成,再打開mysql,切到數據庫你會看到

 

 

 

 

總結:

django   爲咱們內置的數據庫是sqlite ,可是真正生產時是須要mysql, 等大型數據庫的,

好像使用MYSQL,只須要兩步

  • 安裝databases bindings
  • 更改配置文件
相關文章
相關標籤/搜索