日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

orm单表操作

發(fā)布時間:2025/7/25 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 orm单表操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.增加數(shù)據(jù)

from app import models //方式一 book=models.Book.objects.create(name='紅樓夢',price=23.8,publish='人民出版社',author='曹雪芹',create_data='2018-09-17') //方式二 book=models.Book(name='水滸傳',price=99.8,publish='老男孩出版社',author='施耐庵',create_data='2018-08-08') book.save()

2.刪除數(shù)據(jù)

from app import models //方式一 ret=models.Book.objects.filter(name='西游記').delete() //方式二 ret = models.Book.objects.filter(name='西游記').first() ret.delete()

3.修改數(shù)據(jù)

ret=models.Book.objects.filter(name='西游記').update(price=20.9) #對象修改(沒有update方法,但是可以用save來修改) book = models.Book.objects.filter(name='西游記').first() book.price=89 book.save()

4.查詢數(shù)據(jù)

<1> all(): 查詢所有結(jié)果
models.Book.objects.all()
<2> filter(**kwargs): 它包含了與所給篩選條件相匹配的對象(與SQL語句中的where很像)
models.Book.objects.filter(name='西游記').first()
<3> get(**kwargs): 返回與所給篩選條件相匹配的對象,返回結(jié)果有且只有一個,如果符合篩選條件的對象超過一個或者沒有都會拋出錯誤。
ret=models.Book.objects.get(name='紅樓夢') ret=models.Book.objects.get(id=1)
<4> exclude(**kwargs): 它包含了與所給篩選條件不匹配的對象
msg = models.Book.objects.filter(name='龍珠').exists()
<5> order_by(*field): 對查詢結(jié)果排序('-id')
ret=models.Book.objects.all().order_by('price').filter(name='西游記')
<6> reverse(): 對查詢結(jié)果反向排序
ret=models.Book.objects.all().order_by('price'). reverse().filter(name='西游記')
<7> count(): 返回數(shù)據(jù)庫中匹配查詢(QuerySet)的對象數(shù)量。
ret=models.Book.objects.all().count()
<8> first(): 返回第一條記錄
msg = models.Book.objects.filter(name='三國').first()
<9> last(): 返回最后一條記錄
msg = models.Book.objects.filter(name='三國').last()
<10> exists(): 如果QuerySet包含數(shù)據(jù),就返回True,否則返回False
msg = models.Book.objects.filter(name='龍珠').exists()
<11> values(*field): 返回一個ValueQuerySet——一個特殊的QuerySet,運行后得到的并不是一系列model的實例化對象,而是一個可迭代的字典序列
ret=models.Book.objects.all().values('name').distinct()
<12> values_list(*field): 它與values()非常相似,它返回的是一個元組序列,values返回的是一個字典序列
<13> distinct(): 從返回結(jié)果中剔除重復(fù)紀(jì)錄

5.模糊查詢數(shù)據(jù)

1.大于price__gt(查詢價格大于89 的書)
ret=models.Book.objects.filter(price__gt='89')
2.小于price__lt(查詢價格小于89 的書)
ret=models.Book.objects.filter(price__lt='89')
3.小于等于price__lte
ret=models.Book.objects.filter(price__lte='89')
4.大于等于price__gte
ret = models.Book.objects.filter(price__gte='89')
5.在某個列表中__in
ret=models.Book.objects.filter(price__in=['23.8','89','100'])
6.在某個范圍中__range
ret=models.Book.objects.filter(price__range=[50,100])
7.查詢名字有'紅'的書 __contains (類似于SQL中的"%紅%")
ret=models.Book.objects.filter(name__contains='紅')
8.查詢名字帶p的書,忽略大小寫 __icontains
ret=models.Book.objects.filter(name__icontains='P')
9.以XX開頭 __startswith
ret=models.Book.objects.filter(name__startswith='紅')
10.以XX結(jié)尾 __endswith
ret=models.Book.objects.filter(name__endswith='夢')
11.按年查詢 __year
ret=models.Book.objects.filter(create_data__year='2017')

轉(zhuǎn)載于:https://www.cnblogs.com/jianhaozhou/p/9948913.html

總結(jié)

以上是生活随笔為你收集整理的orm单表操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。