我正在尝试构建自己的博客程序 一个功能是让我查看历史这一天的文章帖子 例如。今天是 2021-02-20 然后页面将显示发布于 2020-02-20、2020-01-20、...、2019-02-20 等的文章。
我刚刚发现要设置开始日期和结束日期来查询文章的每个范围(在 python 中):
date_start = datetime(datetime.now().year, datetime.now().month, post_day, 0,0,0,0)
date_end = datetime(datetime.now().year, datetime.now().month, post_day, 23,59,59,999)
posts = DataPost.objects((Q(created_at__gte=date_start) & Q(created_at__lte=date_end)))\
.order_by('-created_at').paginate(page=page, per_page=3)
但是如何查询多个日期,我是否必须在 for 循环中多次查询并合并结果?或者有更聪明的方法吗?
谢谢
答案 0 :(得分:1)
当我使用 mongoengine 时,我尝试了这个:
cond0 = Q()...
for loop:
cond1 = Q(...)
cond0 = cond0 | cond1
DataPost.objects(cond0)
虽然有效,但似乎并不完美。