django orm中如果过滤日期、获取指定日期范围类的数据/ filter gte and lte

前端之家收集整理的这篇文章主要介绍了django orm中如果过滤日期、获取指定日期范围类的数据/ filter gte and lte前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在django实际应用中,经常需要从数据表中提取指定日期范围内的数据。那必须需要使用gte/lte

代码演示:

一般查询

import datetime

start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
article_obj= Article.objects.filter(pub_time__gte=start_date, pub_time__lte=end_date)

使用Q

from django.db.models import Q

article_obj= Article.objects.filter(Q(pub_time__gte=start_date), Q(pub_time__lte=end_date))


猜你在找的Django相关文章