all() 结果为queryset类型
>>> models.Book.objects.all()
<QuerySet [<Book: Book object>, <Book: Book object>, <Book: Book object>]>
filter() 条件查询(它下面也有很多种方法,见最下面)
>>> ret = models.Book.objects.filter(id=5)
>>> ret
<QuerySet [<Book: Book object>]>
get() 得到的是一个model对象,有且只能有一个
会出现两种报错
>>> ret = models.Book.objects.get(id=5)
>>> ret
<Book: Book object>
exclude() 排除
>>> ret = models.Book.objects.all()
>>> ret.exclude(id=5)
<QuerySet [<Book: Book object>, <Book: Book object>]>
order_by() 排序
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() 计数,统计返回结果的数量
>>> ret = models.Book.objects.all().count()
>>> ret
3
first() 返回第一条数据,结果是model对象类型
>>> ret = models.Book.objects.all()
>>> ret.first()
<Book: Book object>
>>> ret.first().id
5
last() 返回最后一条数据,结果是model对象类型
>>> 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)