Django按日期时间过滤日期范围 [英] django filter by datetime on a range of dates

查看:1180
本文介绍了Django按日期时间过滤日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有字段 created_at的模型,并且有一个日期列表。因此,我想获取在日期范围内创建的所有模型。怎么样 ?

I have a model with field "created_at", and I have a list of dates. So, I want to get all the models that are created in the date range. How ?

我知道我们可以使用以下方法轻松比较日期时间和日期:

I know that we can compare datetime and date easily using:

queryset.filter(created_at__startswith=date)

但是,我有一些日期范围,那怎么办?

让我知道更多信息。

But, I have a range of dates, so how ?
Let me know for more information.

推荐答案

您可以使用范围查找。只需找到最低和较大的日期,然后将其应用为:

You can use range lookup. Just find the lowest and greater date and then apply it as:

queryset.filter(created_at__range=(start_date, end_date))

这篇关于Django按日期时间过滤日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆