mysql 連接 django


版本:

django:1.11.9

python3

mysql 5.7.18

在這里我們認為你已經安裝好了mysql,python ,django

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

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

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

first

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

常見的有mysqldb、pymysql、mysqlclient  

  • 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
  • 更改配置文件

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM