9

Django笔记十五之in查询及date日期相关过滤操作 - XHunter

 1 year ago
source link: https://www.cnblogs.com/hunterxiong/p/17284700.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Django笔记十五之in查询及date日期相关过滤操作

这一篇介绍关于范围,日期的筛选

  1. range
  2. weekday
  3. quarter

in 对应于 MySQL 中的 in 操作,可以接受数组、元组等类型数据作为参数:

Blog.objects.filter(id__in=[1,2,3])

对应的 SQL 是:

select * from blog_blog where id in (1,2,3);

字符串也可以作为参数,但是转义后的 SQL 的意思不太一样:


Blog.objects.filter(name__in="abc")

对应的 SQL 为:

select * from blog_blog where name in ('a', 'b', 'c');

in 操作也可以用于子查询动态的获取列表信息:

inner_qs = Blog.objects.filter(name__contains='hunter')
Entry.objects.filter(blog__in=inner_qs)

注意,以上的QuerySet 只在第二步的时候会去查询数据库,其SQL类似于:

select * from blog_entry where blog_id in (select id from blog_blog where name like binary '%hunter%')

在第一条语句中,没有指定字段,所以是默认搜索 id 的值

如果需要搜索其他字段,比如 name,则需要使用 values() 函数或者 values_list 来指定字段:

inner_qs = Blog.objects.filter(name__contains='hunter').values('name')
Entry.objects.filter(blog__name__in=inner_qs)

对应的 SQL 为:

select * from blog_entry where  FROM `blog_entry` INNER JOIN `blog_blog` ON (blog_entry.id = blog_blog.id)
where blog_blog.name in (select name from blog_blog where name like binary '%hunter%')

2、range

range 是在什么范围之内,对应于 SQL 中的 between and

可以作用在 整型和日期类型字段:


Blog.objects.filter(id__range=[1, 10])

Entry.objects.filter(pud_date__range=['2020-01-01', '2023-01-01'])

3、date

获取符合条件的日期,作用在 datetimefield 上,因为 datefield 本身就是日期,所以不需要 __date 开修饰。

假设一个 model 为 TestModel:

class TestModel(models.Model):
	created_time = models.DateTimeField()

这个时候我们想要筛选某个日期,则可以如下使用:

TestModel.objects.filter(created_time__date='2022-01-01')

也可以和上一篇笔记中的 大小于结合使用:

TestModel.objects.filter(created_time__date__gte='2022-01-01')

4、year

筛选年份符合的数据,可作用于 datefield 和 datetimefield 上:

Entry.objects.filter(pub_date__year=2022)

对应的 SQL 为:

select * from blog_entry where pub_date between '2022-01-01' and '2022-12-31';

使用大小于操作:

Entry.objects.filter(pub_date__year__gte=2022)

对应的 SQL 是:

select * from blog_entry where pub_date >= '2022-01-01'

同样的,与 year 相对应的 month 和 day 也可以这样使用。

5、week

一年有 52 个周,如果想获取周数为第几个的话,可以如此操作:

Entry.objects.filter(pub_date__week=2)

对应的 SQL 为:

select * from blog_entry where WEEK(pub_date, 3) = 3;

6、weekday

weekday 对应的是 周几,周日是1,周一是2,到周六是 7

比如想找出 Entry 里所有周一的数据:

Entry.objects.filter(pub_date__weekday=2)

或者是想找出 2022年所有周一的数据:

Entry.objects.filter(pub_date__year=2022).filter(pub_date__weekday=2)

7、quarter

quarter 是查询季度数据,一年一共四个季度,1月1日到3月31日,4月1日到6月30日,7月1日到9月30日,10月1日到12月31日

比如查看所有第二季度的数据:

Entry.objects.filter(pub_date__quarter=2)

8、hour

hour 是小时,与上面的用法同:

TestModel.objects.filter(created_time__hour=9)

以上就是本篇笔记全部内容,接下来将介绍 Django 的 model 的聚合 aggregate 的用法。

本文首发于本人微信公众号:Hunter后端

原文链接:Django笔记十五之in查询及date日期相关过滤操作

如果想获取更多相关文章,可扫码关注阅读:

image

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK