django的ORM操作 增加和查詢
ORM 對(duì)象關(guān)系映射
在數(shù)據(jù)庫(kù)中,實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪改查,使用的是SQ語(yǔ)句,
在django中,通過(guò)python代碼,實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增刪改查,這就是ORM。
在python中,用類名 代表 django數(shù)據(jù)庫(kù)的表名,
用對(duì)象 ,代表django數(shù)據(jù)庫(kù)的一條記錄,
ORM 就是封裝了SQ語(yǔ)句,給對(duì)象進(jìn)行增刪改查,實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的操作,
在settings 文件中,默認(rèn)了splite的數(shù)據(jù)庫(kù),自己可以修改
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}<br data-filtered="filtered"><br data-filtered="filtered">BASE_DIR 是代表當(dāng)前的文件夾 settings,
django對(duì)數(shù)據(jù)庫(kù)的遷移,只需要修改配置即可,
===
在model文件中,設(shè)計(jì)表,,
from django.db import models # Create your models here. #繼承來(lái)自models , class Book(models.Model): name = models.CharField(max_length=32) price = models.IntegerField() Date = models.DateField() auth = models.CharField(max_length=32) publish = models.CharField(max_length=32)
命令臺(tái)執(zhí)行命令。
python manage.py makemigrations python manage.oy migrate
生成數(shù)據(jù)庫(kù)表
在template ,寫一個(gè)主頁(yè)index.html文件,用于顯示添加后書籍
{% load staticfiles %} # ------引用靜態(tài)文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static '/bootstrap-3.3.7/dist/css/bootstrap.css/' %}" rel="external nofollow" > # ---導(dǎo)入bootstrap 文件
<style>
.container{
margin-top: 50px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-2">
<a href="/addbook/" rel="external nofollow" ><button class="btn btn-primary">添加書籍</button></a>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>書名</th>
<th>價(jià)格</th>
<th>出版日期</th>
<th>作者</th>
<th>出版社</th>
</tr>
{# <tr>#}
{# <td>1</td>#}
{# <td>水滸城</td>#}
{# <td>110</td>#}
{# <td>2011.1.1</td>#}
{##}
{# <td>egon</td>#}
{# <td>人民出版社</td>#}
{# </tr>#}
{# <tr>#}
{# <td>2</td>#}
{# <td>水滸城</td>#}
{# <td>110</td>#}
{# <td>2011.1.1</td>#}
{##}
{# <td>egon</td>#}
{# <td>人民出版社</td>#}
{# </tr>#}
{% for book in book_list %} #--從數(shù)據(jù)庫(kù)取到的數(shù)據(jù)是一個(gè)QuerySet集合,進(jìn)行for循環(huán),遍歷出每個(gè)對(duì)象,
<tr>
<td>{{ book.id }}</td> #---把每個(gè)對(duì)象的屬性的值渲染出來(lái),
<td>{{ book.name }}</td>
<td>{{ book.price }}</td>
<td>{{ book.Date }}</td>
<td>{{ book.auth }}</td>
<td>{{ book.publish }}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
</body>
<script>
</script>
</html>
寫一個(gè)addbook.html 文件,用于添加書籍,然后跳轉(zhuǎn)到index頁(yè)面
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static '/bootstrap-3.3.7/dist/css/bootstrap.css/' %} " rel="external nofollow" >
<style>
.container{
margin-top: 50px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-2">
{# {{ csrf-token }}#}
<form class=addbook" action="/addbook/" method="post">
{# 當(dāng)點(diǎn)擊添加按鈕時(shí),是執(zhí)行視圖函數(shù),在數(shù)據(jù)庫(kù)中添加一條記錄,然后再把這個(gè)記錄添加到index頁(yè)面#}
<div class="form-group">
<label for="bookname">書名:</label>
<input type="text" class="form-control" id="bookname" name="bookname"> #---input 中的name屬性,用于獲取輸入的值,
</div>
<div class="form-group">
<label for="price">價(jià)格:</label>
<input type="text" class="form-control" id="price" name="price"> #---input 中的name屬性,用于獲取輸入的值,
</div>
<div class="form-group">
<label for="Date">日期:</label>
<input type="text" class="form-control" id="Date" name="Date"> #---input 中的name屬性,用于獲取輸入的值,
</div>
<div class="form-group">
<label for="auth">作者:</label>
<input type="text" class="form-control" id="auth" name="auth"> #---input 中的name屬性,用于獲取輸入的值,
</div>
<div class="form-group">
<label for="publish">出版社:</label>
<input type="text" class="form-control" id="publish" name="publish"> #---input 中的name屬性,用于獲取輸入的值,
</div>
<input class="btn btn-info" type="submit" value='添加'>
</form>
</div>
</div>
</div>
</body>
</html>
在url文件中,匹配index頁(yè)面和addbook頁(yè)面的視圖函數(shù)
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/$',views.index), url(r'^addbook/$',views.addbook), ]
在views文件中,寫邏輯代碼。用戶訪問(wèn)index頁(yè)面,點(diǎn)擊添加按鈕,跳到addbook頁(yè)面添加數(shù)據(jù)后,提交后,存到數(shù)據(jù)庫(kù),再?gòu)臄?shù)據(jù)庫(kù)拿到數(shù)據(jù)顯示到index頁(yè)面
步驟1,在index頁(yè)面要顯示數(shù)據(jù)庫(kù)的信息,就要導(dǎo)入Book表,獲取所有的數(shù)據(jù),return render 進(jìn)行渲染到頁(yè)面展示
步驟2 ,addbook函數(shù),從form表單中拿到添加的數(shù)據(jù),從request里,以post的方法拿到,
存到數(shù)據(jù)庫(kù),用create方法,表名.objects .create(數(shù)據(jù)庫(kù)的字段名= 表單中的name的屬性名)
Book.objects.create(name = bookname,price = price, Date = Date, auth = auth , publish = publish)
左邊的name 是models里的字段名,對(duì)應(yīng)到form里取到的值,
from django.shortcuts import render,redirect
# Create your views here.
from app01.models import Book
def index(request):
#把數(shù)據(jù)庫(kù)的數(shù)據(jù)嵌入到頁(yè)面進(jìn)行顯示
#查詢api,所有書籍
book_list = Book.objects.all() #數(shù)據(jù)類型是QuerySet:[book1,book2...]
return render(request,'index.html',locals())
def addbook(request):
#是form提交post的方法,
if request.method == 'POST':
#從form表單取數(shù)據(jù)
bookname = request.POST.get('bookname')
price = request.POST.get('price')
Date = request.POST.get('Date')
auth = request.POST.get('auth')
publish = request.POST.get('publish')
#把數(shù)據(jù)存到數(shù)據(jù)庫(kù),先把models 中的表引導(dǎo)到views文件
#方法1,左邊的name 是models里的字段名,對(duì)應(yīng)到form里取到的值,
Book.objects.create(name = bookname,price = price, Date = Date, auth = auth , publish = publish)
#存到數(shù)據(jù)庫(kù)后要放到index頁(yè)面,但現(xiàn)在是addbook頁(yè)面,所以要跳轉(zhuǎn)
return redirect('/index/')
return render(request,'addbook.html')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python使用ctypes調(diào)用dll遇到的坑解決記錄
這篇文章主要為大家介紹了python使用ctypes調(diào)用dll遇到的坑解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Python實(shí)現(xiàn)隱馬爾可夫模型的前向后向算法的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)隱馬爾可夫模型的前向后向算法,本文通過(guò)實(shí)例代碼給大家講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Python 根據(jù)相鄰關(guān)系還原數(shù)組的兩種方式(單向構(gòu)造和雙向構(gòu)造)
本文主要介紹了Python 根據(jù)相鄰關(guān)系還原數(shù)組的兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
python之matplotlib學(xué)習(xí)繪制動(dòng)態(tài)更新圖實(shí)例代碼
這篇文章主要介紹了python之matplotlib學(xué)習(xí)繪制動(dòng)態(tài)更新圖實(shí)例代碼,文中涉及具體實(shí)現(xiàn)代碼,演示效果及運(yùn)行時(shí)出現(xiàn)的問(wèn)題分析等相關(guān)內(nèi)容,小編覺(jué)得還是挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下2018-01-01
使用PyCharm安裝pytest及requests的問(wèn)題
這篇文章主要介紹了使用PyCharm安裝pytest及requests的相關(guān)資料,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07

