Django数据库查询:如何按日期范围过滤对象? [英] Django database query: How to filter objects by date range?
本文介绍了Django数据库查询:如何按日期范围过滤对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个模型中有一个字段,如
I've got a field in one model like
class Sample(models.Model):
date = fields.DateField(auto_now=False)
现在,我需要通过数据范围过滤对象例如,2011年1月1日至2011年1月31日期间的所有对象?
Now, I need to filter the objects by a data range, for example, all the objects that has date between 1 Jan 2011 to 31 Jan 2011?
感谢您的帮助!
推荐答案
使用
Sample.objects.filter(date__range=["2011-01-01", "2011-01-31"])
或者如果你只是尝试过滤月份:
Or if you are just trying to filter month wise:
Sample.objects.filter(date__year='2011',
date__month='01')
编辑
正如Bernhard Vallant所说,如果你想要一个不包括指定范围的查询结束
,你应该考虑他的解决方案,它利用了gt / lt(大于/小于)。
Edit
As Bernhard Vallant said, if you want a queryset which excludes the specified range ends
you should consider his solution, which utilizes gt/lt (greater-than/less-than).
这篇关于Django数据库查询:如何按日期范围过滤对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文