手動建立一個MySQL數據庫python
配置數據庫mysql
ENGINE MySQL NAME 數據庫的名字 HOST ip PORT 3306 USER 用戶名 PASSWORD 密碼
在與settings同級目錄下的__init__.py
中寫代碼:sql
import pymysql pymysql.install_as_MySQLdb()
寫models:數據庫
form django.db import models class Publisher(models.Model): name = models.CharField(max_length=32)
執行遷移的命令django
python manage.py makemigrations # 檢查已經註冊的APP下面的models.py的變動狀況 python manage.py migrate # 執行遷移
ORMapp
對象關係映射spa
對應關係:code
類 _> 表orm
對象 _> 數據行(記錄)對象
屬性 _> 字段
ORM能作的事情:對數據作修改、對錶作修改
from django.db import models class Publisher(models.Model): name = models.CharField(max_length=32) # 一對多的關係 class Book(models.Model): name = models.CharField(max_length=32) pub = models.ForeignKey(to='Publisher',on_delete=None) def __str__(self): return self.name
from app01 import models models.Publisher.objects.all() #查詢全部的數據 QuerySet 對象列表 models.Publisher.objects.get(name='xxx') #查詢一條數據 只能查有且惟一的數據 models.Publisher.objects.filter(name='xxx') #查詢全部知足條件的數據 對象列表 for book in all_books: print(book) print(book.id) print(book.pk) print(book.name) print(book.pub) # 所關聯的出版社對象 對象.外鍵 print(book.pub_id) # 所關聯的出版社對象的id 對象.外鍵_id
obj = models.Publisher.objects.create(name='xxxx') models.Book.objects.create(name=book_name, pub=models.Publisher.objects.get(pk=pub_id)) obj = models.Book.objects.create(name=book_name, pub_id=pub_id) obj = models.Publisher(name='xxxx') obj.save() # 保存到數據庫 obj = models.Book(name='xxx',pub_id=出版社的對象id) obj.save() 多對多的新增: 書對做者 book_id = request.POST.getlist('book_id') # 插入數據 obj = models.Author.objects.create(name=author_name) obj.books.set(book_id) # 設置多對多的關係
models.Publisher.objects.filter(pk=pk).delete() # 批量刪除 models.Publisher.objects.get(pk=pk).delete() # 單條數據的刪除
models.Book.objects.filter(pk=pk).update(name=book_name,pub_id=pub_id) # 批量更新 obj = models.Book.objects.filter(pk=1).first() obj.name = 'xxxx' obj.pub_id = 2 # book_obj.pub = 出版社的對象 obj.save() #保存更新
class Book(models.Model): name = models.CharField(max_length=32) pub = models.ForeignKey('Publisher', on_delete=models.CASCADE) """ on_delete 在2.0版本以後是必填的 on_delete= models.CASCADE 級聯刪除 PROTECT 保護 SET(1) SET_DEFAULT 設置爲默認值設置爲某一個值 SET_NULL 設置爲空 DO_NOTHING 什麼都不變 """
class Pulisher(models.Model): name = models.CharField(max_length=32) class Book(models.Model): name = models.CharField(max_length=32) pub = models.ForeignKey('Pulisher',on_delete=models.DO_NOTHING) class Author(models.Model): name = models.CharField(max_length=32) books= models.ManyToManyField(to='Book')
# 查詢 book_obj.pub # 所關聯的對象 book_obj.pub_id 所關聯的對象的id author_obj.books # 關係管理對象 author_obj.books.all() # 所關聯的全部的書籍對象
# 新增 Book.objects.create(name='xxx',pub=對象) Book.objects.create(name='xxx',pub_id=對象的ID) obj= Book(name='xxx',pub_id=對象的ID) obj.save() obj = Author.objects.create(name='xxx') obj.books.set([書籍id,書籍id])
# 刪除 Book.objects.filter(pk=pk).delete() # QuerySet 刪除 Author.objects.get(pk=pk).delete() # 對象 刪除
# 編輯 Book.objects.filter(pk=pk).update(name='xxx') book_obj.name ='xxxx' book_obj.save() Author.objects.filter(pk=pk).update(name='xxx') author_obj.books.set([id,id])