all() 結果爲queryset類型html
>>> models.Book.objects.all() <QuerySet [<Book: Book object>, <Book: Book object>, <Book: Book object>]>
filter() 條件查詢(它下面也有不少種方法,見最下面)python
>>> ret = models.Book.objects.filter(id=5) >>> ret <QuerySet [<Book: Book object>]>
get() 獲得的是一個model對象,有且只能有一個 會出現兩種報錯mysql
>>> ret = models.Book.objects.get(id=5) >>> ret <Book: Book object>
exclude() 排除sql
>>> ret = models.Book.objects.all() >>> ret.exclude(id=5) <QuerySet [<Book: Book object>, <Book: Book object>]>
order_by() 排序django
models.Book.objects.all().order_by('-price','id') #相似於mysql種的order by price desc,id asc;
reverse() 反轉數組
models.Book.objects.all().order_by('id').reverse() # 數據排序以後才能反轉
count() 計數,統計返回結果的數量spa
>>> ret = models.Book.objects.all().count() >>> ret 3
first() 返回第一條數據,結果是model對象類型code
>>> ret = models.Book.objects.all() >>> ret.first() <Book: Book object> >>> ret.first().id 5
last() 返回最後一條數據,結果是model對象類型htm
>>> ret = models.Book.objects.all() >>> ret.last() <Book: Book object> >>> ret.last().id 11
exists() 判斷返回結果集是否是有數據對象
>>> ret = models.Book.objects.filter(id=999).exists() >>> ret False >>> ret = models.Book.objects.filter(id=11).exists() >>> ret True
values() (返回的queryset,裏面是字典類型數據)
>>> ret = models.Book.objects.all().values('id','book_name') >>> ret <QuerySet [{'id': 5, 'book_name': '1'}, {'id': 6, 'book_name': '2'}, {'id': 11, 'book_name': '23'}]>
values_list(返回的queryset,裏面是數組類型數據)
>>> ret = models.Book.objects.all().values_list('id','book_name') >>> ret <QuerySet [(5, '1'), (6, '2'), (11, '23')]>
調用values或者values_list的是objects控制器,那麼返回全部數據
>>> ret = models.Book.objects.values() >>> ret <QuerySet [ {'id': 5, 'book_name': '1', 'price': 2.0, 'shijian': '2018-03-22', 'press': 'admin'}, {'id': 6, 'book_name': '2', 'price': 1234.0, 'shijian': '2013-3-2', 'press': 'sdf'}, {'id': 11, 'book_name': '23', 'price': 11.0, 'shijian': '2019-09-10', 'press': '234'} ]>
distinct() 去重,配置values和values_list來使用
>>> models.Book.objects.all().values('book_name').distinct() <QuerySet [{'book_name': '1'}, {'book_name': '2'}, {'book_name': '23'}]>
__in price值等於這三個裏面的任意一個的對象
Book.objects.filter(price__in=[100,200,300])
__gt 大於
>>> ret = models.Book.objects.filter(price__gt=1233) >>> ret.values() <QuerySet [{'id': 6, 'book_name': '2', 'price': 1234.0, 'shijian': '2013-3-2', 'press': 'sdf'}]>
__gte大於等於
>>> ret = models.Book.objects.filter(price__gte=2) >>> ret.count() 3
lt 等於
>>> ret = models.Book.objects.filter(price__lt=1234) >>> ret.count() 2
lte 小於等於
>>> ret = models.Book.objects.filter(price__lte=1234) >>> ret.count() 3
__range 大於等於35,小於等於38
>>>ret = models.Book.objects.filter(price__range=[35,38]) >>>ret.count() 3
__contains 包含這個字符串的數據
>>> ret = models.Book.objects.filter(book_name__contains='2') >>> ret.values() <QuerySet [{'id': 6, 'book_name': '2', 'price': 1234.0, 'shijian': '2013-3-2', 'press': 'sdf'}, {'id': 11, 'book_name': '23', 'price': 11.0, 'shijian': '2019-09-10', 'press': '234'}]>
__icontains 不區分大小寫
ret = models.Book.objects.filter(book_name__icontains='py')
__startswith 以什麼開頭 -- endswith(以什麼結尾) -- istartswith(不區分大小寫)
>>> ret = models.Book.objects.filter(book_name__startswith='2') >>> ret.values() <QuerySet [{'id': 6, 'book_name': '2', 'price': 1234.0, 'shijian': '2013-3-2', 'press': 'sdf'}, {'id': 11, 'book_name': '23', 'price': 11.0, 'shijian': '2019-09-10', 'press': '234'}]>
__year 查詢年 __month 月
ret = models.Book.objects.filter(publish_date__year='2018')
__isnull 字段值爲空的數據
models.Book.objects.filter(publish_date__isnull=True)
原文出處:https://www.cnblogs.com/alex3174/p/11608374.html