Py学习  »  Django

django-orm查询条件的使用

陈明勇1999 • 3 年前 • 394 次点击  

常用条件

exact

使用 exact 相当于是SQL层面的=,如果赋值为None,则在SQL层面将被解释为null。

book = Book.objects.filter(id__exact=1)
book = Book.objects.filter(id__exact=None)
  • 1
  • 2
  • 1
  • 2

以上两条查找语句在SQL底层的语句为:

select * from book where id=1;
select * from book where id IS NULL;
  • 1
  • 2
  • 1
  • 2

iexact

使用 iexact 相当于是SQL层面的like。

book = Book.objects.filter(id__iexact=1)
  • 1
  • 1

以上查找语句在SQL底层的语句为:

select * from book where id like '1';
  • 1
  • 1

contains

大小写敏感的匹配查询,相当于like,语句转化后两边都有%。

book = Book.objects.filter(name__contains='Hello')
  • 1
  • 1

以上查找语句在SQL底层的语句为:

select * from book where name like binary '%Hello%';
  • 1
  • 1

icontains

大小写不敏感的匹配查询,相当于like,语句转化后两边都有%。

book = Book.objects.filter(name__icontains='Hello')
  • 1
  • 1

以上查找语句在SQL底层的语句为:

select * from book where


    
 name like '%Hello%';
  • 1
  • 1

in

查询给定的field的值是否在给定的容器中。容器可以为list、tuple或者任何一个可以迭代的对象,包括QuerySet对象。

book = Book.objects.filter(id__in=[1, 2, 3])
  • 1
  • 1

以上查找语句在SQL底层的语句为:

select * from book where id IN (1,2,3);
  • 1
  • 1

QuerySet 对象查询:

book = Book.objects.filter(id__in=[1, 2, 3])
publisher = Publisher.objects.filter(book__in=book)
  • 1
  • 2
  • 1
  • 2

range

判断某个field的值是否在给定区间中。

# 查询书籍id为1-4的书籍
books = Book.objects.filter(id__range=(1, 4))
# 根据时间段查询
from datetime import datetime
start_time = datetime(year=2021, month=1, day=2)
end_time = datetime(year=2021, month=1, day=9)
books = Book.objects.filter(pub_time__range=(start_time, end_time))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

以上两个查找语句在SQL底层的语句为:

select * from book where id between 1 and 4;
select * from book where pub_time between '2021-01-02 00:00:00' and '2021-01-09 00:00:00';
  • 1
  • 2
  • 1
  • 2

date

针对某些 date 或者 datetime 类型的字段进行查询。

# 方式1
from datetime import datetime
books = Book.objects.filter(pub_time__date=datetime(year=2021, month=1, day=9))
# 方式2
books =


    
 Book.objects.filter(pub_time__date='2021-01-09')
# 查询出版时间为2021-01-02之后的书籍
books = Book.objects.filter(pub_time__date__gt='2021-01-02')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

以上查找语句在SQL底层的语句都为:

# 方式1和方式2
SELECT * FROM `book` WHERE DATE(`pub_time`) = '2021-01-09';
# 查询出版时间为2021-01-02之后的书籍
SELECT * FROM `book` WHERE DATE(`pub_time`) > '2021-01-02';
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

year

根据年份进行查找。

# 查询出版时间为2021年的书籍
books = Book.objects.filter(pub_time__year='2021')
# 查询出版时间在2020年之后的书籍
books = Book.objects.filter(pub_time__year__gt='2020')
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

以上查找语句在SQL底层的语句都为:

# 查询出版时间为2021年的书籍
select * from book where pub_time BETWEEN '2021-01-01 00:00:00' AND '2021-12-31 23:59:59.999999';
# 查询出版时间在2020年之后的书籍
select * from book where pub_time > '2020-12-31 23:59:59.999999';
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

time

根据时间来查找。

from datetime import time
books = Book.objects.filter(pub_time__time=time(hour=9, minute=22, second=33))
  • 1
  • 2
  • 1
  • 2

以上查找语句在SQL底层的语句都为:

select * from book where TIME(`pub_time`) = '09:22:33';
  • 1
  • 1

关联表查询

publisher = Publisher.objects.filter(book__id__in=[1, 2


    
, 3])
  • 1
  • 1

以上查找语句在SQL底层的语句为:

SELECT * FROM `publisher` INNER JOIN `book` ON (`publisher`.`id` = `book`.`publisher_id`) WHERE `book`.`id` IN (1, 2, 3);
  • 1
  • 1

比较运算

gt

查询某个filed的值大于给定值。

# 查询id>2的书籍
books = Book.objects.filter(id__gt=2)
  • 1
  • 2
  • 1
  • 2

以上查找语句在SQL底层的语句为:

select * from book where id > 2;
  • 1
  • 1

gte

查询某个filed的值大于等于给定值。

lt

查询某个filed的值小于给定值。

lte

查询某个filed的值小于等于给定值。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/105887
 
394 次点击