在前面(【實戰(zhàn)演練】數(shù)據(jù)庫基本知識與原理系列https://blog.51cto.com/14423403/2418820)的文章,已經(jīng)分享過數(shù)據(jù)庫的原理,設計與開發(fā)的范式,以及根據(jù)我們django項目的需求,進行了數(shù)據(jù)庫的設計。另外也介紹過數(shù)據(jù)庫操作的基本SQL命令。
以前不使用web框架來進行開發(fā),那么就需要在一個php或者py文件(頁面文件里面),從展示層(html、css、js)到邏輯層(php、python)到數(shù)據(jù)層(SQL)的東西都要寫。直接用pymysql用什么pymysql.connect()連接數(shù)據(jù)庫,然后用pymysql.fetchall()在里面寫具體sql語句來操作數(shù)據(jù)庫。
1、django數(shù)據(jù)庫OCM
而這樣的最大問題是展示層、邏輯層、數(shù)據(jù)層沒有分離,每個開發(fā)人員都要從前端到后端全部懂,并且代碼很難復用,所以引用了MVC模型的開發(fā)框架將3者分開。
此外,django里面有一套叫OCM的操作命令,來封裝了SQL語句,不用具體寫SQL命令,這樣有什么好處呢?
不同版本數(shù)據(jù)庫SQL有微小差異:mysql、oracle、sql server總體上sql的增刪改查命令都是一樣的,但是細節(jié)上還是有一些小差異的。所以如果需要將系統(tǒng)從mysql數(shù)據(jù)庫遷移到oracle數(shù)據(jù)庫,那么很可能需要對代碼進行重寫,否則會出BUG。
而django直接使用OCM命令封裝SQL,其實OCM就已經(jīng)自動匹配與翻譯SQL命令,在settings.py文件里面,就可以指定django項目使用什么數(shù)據(jù)庫,然后執(zhí)行
python?manage.py?makemigrations python?manage.py?migrate
系統(tǒng)就會自動生產(chǎn)對應配置了的數(shù)據(jù)庫(例如oracle)的SQL命令,并且對數(shù)據(jù)庫進行操作。
2、編輯models
只需要編輯app里面的models.py文件,就可以創(chuàng)建數(shù)據(jù)庫表了。
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
from
django.db
import
models
# Create your models here.
class
student(models.Model):
? ?sno = models.CharField(
max_length
=
10
,
unique
=
True
,
primary_key
=
True
)
? ?sname = models.CharField(
max_length
=
10
,
null
=
True
)
? ?ssex = models.CharField(
max_length
=
10
,
null
=
True
)
? ?susername = models.CharField(
max_length
=
20
,
null
=
True
)
? ?sage = models.CharField(
max_length
=
10
,
null
=
True
)
? ?sdept = models.CharField(
max_length
=
10
,
null
=
True
)
class
teacher(models.Model):
? ?tno = models.CharField(
max_length
=
10
,
unique
=
True
,
primary_key
=
True
)
? ?tname = models.CharField(
max_length
=
10
,
null
=
True
)
? ?tusername = models.CharField(
max_length
=
20
,
null
=
True
)
? ?ttitle = models.CharField(
max_length
=
10
,
null
=
True
)
? ?
class course(models.Model):
? ?cno = models.CharField(max_length=10, unique=True, primary_key=True)
? ?cname = models.CharField(max_length=10, null=True)
? ?ccredit = models.CharField(max_length=10, null=True)
? ?ctime = models.CharField(max_length=10, null=True)
? ?cplace = models.CharField(max_length=10, null=True)
? ?tno_id = models.ForeignKey(teacher,to_field='tno',on_delete=models.CASCADE)
class score(models.Model):
? ?cno = models.ForeignKey(course, to_field='cno', on_delete=models.CASCADE)
? ?sno = models.ForeignKey(student, to_field='sno', on_delete=models.CASCADE)
? ?cscore = models.IntegerField(null=True)
每個字段可以按照需要自定義類型,具體可以查閱官方文檔,其中CharField是字符串,DateFiled是時間戳,IntergeFiled是整數(shù),foreignkey就是外鍵,CASCADE是級聯(lián)刪除,即如果主鍵刪除了,要級聯(lián)刪除。部分django版本寫成on_delete='CASCADE'
系統(tǒng)為什么要創(chuàng)建這些表格,怎么定義表格的字段與關聯(lián)關系,詳見之前的文章:(【實戰(zhàn)演練】數(shù)據(jù)庫基本知識與原理系列02-數(shù)據(jù)庫設計與開發(fā)的范式https://blog.51cto.com/14423403/2418820)
然后執(zhí)行makemigrations,是將models里面的數(shù)據(jù)庫表創(chuàng)建的需求,轉(zhuǎn)為對應mysql配置的SQL命令。
python?manage.py?makemigrations
然后對配置好的數(shù)據(jù)庫進行轉(zhuǎn)換后的命令操作。
python?manage.py?migrate
執(zhí)行完畢后,直接用navicat for mysql去數(shù)據(jù)庫查看,數(shù)據(jù)庫表已經(jīng)成功創(chuàng)建了。
3、OCM操作數(shù)據(jù)庫
django使用OCM對數(shù)據(jù)庫進行操作,當然也是增、刪、查、改、連接5大類操作。
instance按照實際修改,例如編輯student表,那么instance就是models.student代替。
3.1、增
instance.objects.save() instance.objects.create(username='張三',pwd='123456')
3.2、刪
instance.objects.filter(username='張三').delete()
3.3、查
instance.objects.all() instance.objects.all().values('user')????#只取user列 instance.objects.all().values_list('id','user')????#取出id和user列,并生成一個列表 instance.objects.get(id=1)??#get返回的是對象 instance.objects.filter(id=1)??#filter是返回一個id=1的一整行,返回的值是一個序列。
3.4、改
instance.objects.filter(username='zhangsan').update(pwd='123456')
3.5、連接
OCM的連接會比SQL更加方便,只要建立數(shù)據(jù)庫表的時候做好了foreign外鍵的連接,就會自動做好關聯(lián),只需要輸入當前查詢的表格所直接關聯(lián)的表名雙下劃線字段名就可以了。例如當前查詢的是teacher表,那么與之直接關聯(lián)的是course表,所以可以filter對teacher表字段進行篩選,然后要返回的values,course的字段可以直接用course__sno來表示,而course表與score表做了關聯(lián),所以如果要顯示score表的字段,需要用course__score_sno來表示。
data = teacher.objects.filter(
tusername
=username).values(
'course__cno'
,
'course__cname'
,
'course__score__sno__sname'
,
'course__score__cscore'
,
'course__score__sno__sno'
)
懂得OCM操作數(shù)據(jù)庫后,我們可以開始動態(tài)網(wǎng)頁的開發(fā)工作了。
4、OCM操作數(shù)據(jù)庫
為了方便測試與掌握OCM操作數(shù)據(jù)庫技巧,我們現(xiàn)在models.py里面增加一張用戶名密碼表,用戶登錄頁面的操作。(后期不用這張表)
testuser(models.Model): ????username?=?models.CharField(=) ????password?=?models.CharField(=)
python?manage.py?makemigrations python?manage.py?migrate
用navicat for mysql手工在testuser表里面創(chuàng)建一個測試用戶。
然后回到之間的index.html靜態(tài)文件里面,對于
并且submit的按鈕的type改為submit。
在views里面導入models里面的對象。
models?*
修改原來的index函數(shù),編寫業(yè)務邏輯。
通過request.POST.get()可以獲取前端頁面POST過來的變量的值。
通過testuser.objects.get(username=username)可以查詢數(shù)據(jù)庫,前面的username是testuser的字段名,后面的username是request.POST.get獲取值回來后賦值到username的這個變量。
如果查詢到有數(shù)據(jù),賦值給userinfo,userinfo.password表示密碼的這個數(shù)據(jù),用if做對比后,為真,則返回登陸后的頁面,否則還是跳轉(zhuǎn)到當前頁面。
(request): ????request.method?==?request.POST: ????????username?=?request.POST.get() ????????password?=?request.POST.get() ????????userinfo?=?testuser.objects.get(=username) ????????userinfo.password?==?password: ????????????render_to_response(()) ????render_to_response(())
至此,可以輸入用戶名密碼進行測試了。
輸入正面的用戶名密碼,點擊提交,用戶名密碼正確后,就會自動跳轉(zhuǎn)。
輸入錯誤的用戶名/密碼,點擊提交后,又回到了當前頁面。
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
