Django:从datetime字段查询时间 [英] Django: Querying time from datetime fields

查看:579
本文介绍了Django:从datetime字段查询时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在o基于postgresql数据库的Django上,如何按时间筛选datetimefield如下?

On o postgresql db based Django, how can I filter by time for a datetimefield as below?

class Foo(models.Model):
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()

IE:我想使用16:30start_date和19:00end_date过滤Foo对象。

IE: I want to filter Foo objects with "16:30" start_date and "19:00" end_date.

谢谢。

推荐答案

如何在TimeField中添加?
http://docs.djangoproject.com/en/ dev / ref / models / fields /#timefield

What about adding in a TimeField? http://docs.djangoproject.com/en/dev/ref/models/fields/#timefield

否则,您需要编写自定义查询才能利用数据库的时间功能,因为DateTimeFields在Django中本地没有这种功能。

Otherwise you would need to write a custom query to take advantage of the database´s time capabilites since DateTimeFields don't have that capability natively in Django.

您可以考虑编写一个函数,将从start_date到小时和分钟的反规范化到一个新的start_time字段,然后查询start_time字段。

You could consider writing a function to denormalize hours and minutes from start_date to a new start_time field and then query the start_time field.

这篇关于Django:从datetime字段查询时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆