如何在python(Django)中基于日期时间范围过滤对象 [英] How to filter an object based on a datetime range in python (Django)
本文介绍了如何在python(Django)中基于日期时间范围过滤对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个包含所有最新帖子的页面.
I am trying to create a page with all the most recent posts.
class Post(models.Model):
title = models.CharField(max_length=40)
postTime = models.DateTimeField(auto_now_add=True)
我找到了这个例子:
start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
Post.objects.filter(postTime__range=(start_date, end_date))
我知道我想使用__range函数.但是如何做到这一点,这样我就可以获取从今天到两周前的所有帖子
I know I'm suppose to use the __range function. But how can I make it so that I can get all the posts from a range of today all the way to two weeks ago
类似这样的东西:
start_date = datetime.date(date from two weeks ago)
end_date = datetime.date(currentdate)
Post.objects.filter(postTime__range(start_date, end_date))
推荐答案
start_date = end_date - datetime.timedelta(days=14)
这篇关于如何在python(Django)中基于日期时间范围过滤对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文