Django从理论到实战(part27)--ORM模型的增删改查操作
學(xué)習(xí)筆記,僅供參考
參考自:Django打造大型企業(yè)官網(wǎng)–Huang Y;
本系列Blog以應(yīng)用為主,理論基礎(chǔ)部分我在后端專欄的Django系列博客已經(jīng)寫過了,如果有些需要補(bǔ)充的知識(shí)點(diǎn),我會(huì)在這個(gè)系列中,盡量詳細(xì)的記錄一下。
ORM模型的增刪改查操作
首先,我們看一下我們的models.py文件中的Book類:
from django.db import models# Create your models here.class Book(models.Model):#定義一個(gè)自增長(zhǎng)的主鍵bookid = models.AutoField(primary_key = True)name = models.CharField(max_length=20,null=False)author = models.CharField(max_length=20,null=False)price = models.FloatField(default=0)我們將基于這個(gè)類對(duì)應(yīng)的數(shù)據(jù)表進(jìn)行增刪改查操作。
對(duì)數(shù)據(jù)庫的增刪改查一系列操作,我們可以在視圖函數(shù)中完成,也可以在Django Shell中進(jìn)行。在下面的例子中,我先利用視圖函數(shù)增加記錄,再用Django Shell進(jìn)行其他數(shù)據(jù)庫操作。
增加記錄
- 在視圖函數(shù)中進(jìn)行添加操作
bookstore應(yīng)用下的views.py文件:
from django.http import HttpResponse from .models import Book# Create your views here.def add_book(request):book = Book(name = "統(tǒng)計(jì)學(xué)", author = "賈俊平", price = 25)book.save()return HttpResponse("圖書添加成功!")bookstore應(yīng)用下的urls.py文件:
from django.contrib import admin from django.urls import path from . import viewsurlpatterns = [path('add_book/', views.add_book), ]主urls.py模塊:
from django.contrib import admin from django.urls import path, includeurlpatterns = [path('admin/', admin.site.urls),path('bookstore/', include("bookstore.urls")), ]向http://127.0.0.1:8000/bookstore/add_book/發(fā)起請(qǐng)求:
查看bookstore_book數(shù)據(jù)表中的記錄:
mysql> select * from bookstore_book; +--------+--------+--------+-------+ | bookid | name | author | price | +--------+--------+--------+-------+ | 1 | 統(tǒng)計(jì)學(xué) | 賈俊平 | 25 | +--------+--------+--------+-------+ 1 row in set (0.01 sec)- 在Django Shell中進(jìn)行添加操作
啟動(dòng)Django Shell:
python manage.py shell添加記錄:
>>> from bookstore.models import Book >>> book = Book(name = "機(jī)器學(xué)習(xí)", author = "周志華", price = 50) >>> book.save()查看bookstore_book數(shù)據(jù)表中的記錄:
mysql> select * from bookstore_book; +--------+----------+--------+-------+ | bookid | name | author | price | +--------+----------+--------+-------+ | 1 | 統(tǒng)計(jì)學(xué) | 賈俊平 | 25 | | 2 | 機(jī)器學(xué)習(xí) | 周志華 | 50 | +--------+----------+--------+-------+ 2 rows in set (0.00 sec)查詢單個(gè)記錄
查詢主鍵值(primary_key)等于1的記錄:
>>> book = Book.objects.get(pk = 1) >>> book <Book: Book object (1)>可以看到book得到了一個(gè)Book類的實(shí)例對(duì)象,這樣的顯示對(duì)于我們而言沒有什么意義,為此,我們可以通過重寫B(tài)ook類的 __str__方法,改變它的返回值:
from django.db import modelsclass Book(models.Model):#定義一個(gè)自增長(zhǎng)的主鍵bookid = models.AutoField(primary_key = True)name = models.CharField(max_length=20,null=False)author = models.CharField(max_length=20,null=False)price = models.FloatField(default=0)def __str__(self):return "[name:{}, author:{}, price:{}]".format(self.name, self.author, self.price)我們重啟Django shell,并重新查詢:
>>> from bookstore.models import Book >>> book = Book.objects.get(pk = 1) >>> book <Book: [name:統(tǒng)計(jì)學(xué), author:賈俊平, price:25.0]>Very Well !
查詢多條數(shù)據(jù)
我們?cè)僭黾訋讞l記錄,并進(jìn)行查詢操作:
>>> books = Book.objects.filter(author='何曉群') >>> for book in books: ... print(book) ... [name:多元統(tǒng)計(jì)分析, author:何曉群, price:25.0] [name:應(yīng)用回歸分析, author:何曉群, price:20.0]修改數(shù)據(jù)
>>> book = Book.objects.get(name='機(jī)器學(xué)習(xí)') >>> book.price = 45 >>> book.save()
查看bookstore_book數(shù)據(jù)表中的記錄:
mysql> select * from bookstore_book; +--------+--------------+--------+-------+ | bookid | name | author | price | +--------+--------------+--------+-------+ | 1 | 統(tǒng)計(jì)學(xué) | 賈俊平 | 25 | | 2 | 機(jī)器學(xué)習(xí) | 周志華 | 45 | | 3 | 多元統(tǒng)計(jì)分析 | 何曉群 | 25 | | 4 | 應(yīng)用回歸分析 | 何曉群 | 20 | +--------+--------------+--------+-------+ 4 rows in set (0.00 sec)刪除數(shù)據(jù)
>>> book = Book.objects.get(name='機(jī)器學(xué)習(xí)') >>> book.delete() (1, {'bookstore.Book': 1})
查看bookstore_book數(shù)據(jù)表中的記錄:
mysql> select * from bookstore_book; +--------+--------------+--------+-------+ | bookid | name | author | price | +--------+--------------+--------+-------+ | 1 | 統(tǒng)計(jì)學(xué) | 賈俊平 | 25 | | 3 | 多元統(tǒng)計(jì)分析 | 何曉群 | 25 | | 4 | 應(yīng)用回歸分析 | 何曉群 | 20 | +--------+--------------+--------+-------+ 3 rows in set (0.00 sec)數(shù)據(jù)排序
按照價(jià)格從小到大對(duì)書籍進(jìn)行排序:
>>> books = Book.objects.order_by("price") >>> for book in books: ... print(book) ... [name:應(yīng)用回歸分析, author:何曉群, price:20.0] [name:統(tǒng)計(jì)學(xué), author:賈俊平, price:25.0] [name:多元統(tǒng)計(jì)分析, author:何曉群, price:25.0]總結(jié)
以上是生活随笔為你收集整理的Django从理论到实战(part27)--ORM模型的增删改查操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。