Python django 在日期范围内按日期时间过滤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20379246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django filter by datetime on a range of dates
提问by user2139745
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 ?
我有一个带有“created_at”字段的模型,我有一个日期列表。所以,我想获取在日期范围内创建的所有模型。如何 ?
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.
但是,我有一个日期范围,那么如何呢?
让我知道更多信息。
回答by Simeon Visser
You can use __gte(greater than or equal) and __lte(less than or equal). For example:
您可以使用__gte(大于或等于)和__lte(小于或等于)。例如:
queryset.filter(created_at__gte=datetime.date.today())
回答by JamesO
回答by Aamir Adnan
回答by user956424
Use https://pypi.python.org/pypi/django-daterange-filter/to use date range filter.
使用https://pypi.python.org/pypi/django-daterange-filter/使用日期范围过滤器。

