我在 Django 模型中使用 postgres DateTimeRangeField。它是现有数据库,所有现有行都有下限和上限。现在我需要查询下限日期为特定日期的所有行,例如 rangefield.lower.date==1 之类的内容。
请不要告诉我为开始日期和结束日期使用单独的字段。该系统正在生产中,我需要查询它以过滤分析。
class Model(models.Model):
rangefield = DateTimeRangeField(db_index=True)
....
我需要查询类似 Model.objects.filter(rangefield__lower__date=1)
最佳答案
您可以使用startswith
查找说明here
Model.objects.filter(rangefield__startswith__day=1)
如果上述方法不起作用,请尝试:
Model.objects.filter(rangefield__startswith__date__day=1)
关于python - 从 Django DateTimeRangeField 按 datetime.now().date 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46653023/