您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)django中怎么操作model,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
單表操作
增加數(shù)據(jù)
auther_obj = {"auther_name":"崔皓然","auther_age":1} models.auther.objects.create( **auther_obj )
models.auther.objects.create( auther_name="周雍博", auther_age=4 )
obj = models.auther( auther_name="崔洪艷", auther_age=29 ) obj.save()
obj = models.auther( auther_name = "王仙枝", auther_age = "50" ) obj.save()
刪除數(shù)據(jù)
models.auther.objects.filter(auther_name="崔洪艷")[0].delete()
一對多操作
增加數(shù)據(jù)
a_obj = models.auther.objects.get(id=1) models.book.objects.create( book_name = "python課程1", book_price = 12, book_auther = a_obj )
a_obj = models.auther.objects.get(id=5) models.book.objects.create( book_name = "java課程1", book_price = 13, book_auther_id = a_obj.id )
a_obj = models.auther.objects.get(id=2) book_dict = { "book_name":"python課程2", "book_price":14, "book_auther_id":a_obj.id } obj = models.book.objects.create( **book_dict )
a_obj = models.auther.objects.get(id=4) book_dict = { "book_name":"java課程2", "book_price":15, "book_auther":a_obj } models.book.objects.create( **book_dict )
一對多正向查詢
# 查詢id為2的書的所有作者的信息 obj = models.book.objects.get(id=2) print(obj.book_auther.auther_name) print(obj.book_auther.auther_age) obj = models.book.objects.filter(id=2).values("book_name","book_price","book_auther__id","book_auther__auther_name","book_auther__auther_age") print(obj)
# 查詢id為2的作者的出版的書 obj = models.book.objects.filter(book_auther_id=2).values("book_name","book_price","book_auther__id","book_auther__auther_name","book_auther__auther_age") print(obj) obj = models.book.objects.get(book_auther_id=2) print(obj.book_auther.auther_age) print(obj.book_auther.auther_name) print(obj.book_name)
一對多反向查詢
# 查看出版社出版的書 obj = models.auther.objects.filter(id=2).values("auther_name","auther_age","book__book_name") print(obj) obj = models.auther.objects.get(id=2).book_set.values("book_name","book_price","book_auther__id","book_auther__auther_name","book_auther__auther_age") print(obj)
多對多操作
增加數(shù)據(jù)
b1 = models.book.objects.get(id=1) b2 = models.book.objects.get(id=2) b4 = models.book.objects.get(id=4) b5 = models.book.objects.get(id=5) models.publish.objects.get(id=1).publish_book.add( b1,b2 )
book_list = [b1,b2] models.publish.objects.get(id=2).publish_book.add( *book_list )
更新數(shù)據(jù)
book_list = [b1,b2,b4,b5] models.publish.objects.get(id=1).publish_book.set(book_list)
移除數(shù)據(jù)
models.publish.objects.get(id=1).publish_book.remove(b1) book_list = [b2, ] models.publish.objects.get(id=1).publish_book.remove(*book_list)
正向查詢
obj = models.publish.objects.get(id=1).publish_book.values("book_name","publish__publish_name") print(obj) obj = models.publish.objects.filter(id=1).values("publish_name","publish_book__book_name") print(obj)
反向添加數(shù)據(jù)
p1 = models.publish.objects.get(id=1) p2 = models.publish.objects.get(id=2) p3 = models.publish.objects.get(id=3) b1.publish_set.add(p1,p2,p3)
反向刪除數(shù)據(jù)
b1.publish_set.remove(p1)
反向更新數(shù)據(jù),把remove替換為set就可以了
反向查詢
obj = models.book.objects.get(id=1).publish_set.values("publish_name","publish_book__book_name")
model中多表關(guān)系和參數(shù)
多對多
一對一
model操作
看完上述內(nèi)容,你們對django中怎么操作model有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。