[Django] #26348: Add a __time lookup for DateTimeField

13 views
Skip to first unread message

Django

unread,
Mar 11, 2016, 6:28:40 PM3/11/16
to django-...@googlegroups.com
#26348: Add a __time lookup for DateTimeField
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: nobody
Type: New | Status: new
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Now that we've added the `__date` (#9596) lookup I think we should allow
the same for the time part.

A good
[https://www.reddit.com/r/django/comments/49p8dg/excluding_hours_range_ex_from_215am_to_5am_from_a/
usage example] would be:

{{{#!python
Event.objects.filter(
datetime__time__range=(start_time, end_time),
)
}}}

To select all activities that occurred in a timezone aware time range.

Here's an implementation for PostgreSQL and MySQL missing tests:

{{{#!python
from django.conf import settings
from django.db import models
from django.utils import timezone
from django.utils.functional import cached_property


class DateTimeTimeTransform(models.Transform):
lookup_name = 'time'

@cached_property
def output_field(self):
return models.TimeField()

def as_mysql(self, compiler, connection):
lhs, lhs_params = compiler.compile(self.lhs)
if settings.USE_TZ:
lhs = "CONVERT_TZ(%s, 'UTC', %%s)" % lhs
tzname = timezone.get_current_timezone_name()
lhs_params.append(tzname)
sql = "TIME(%s)" % lhs
return sql, lhs_params

def as_postgresql(self, compiler, connection):
lhs, lhs_params = compiler.compile(self.lhs)
if settings.USE_TZ:
lhs = "%s AT TIME ZONE %%s" % lhs
tzname = timezone.get_current_timezone_name()
lhs_params.append(tzname)
sql = "(%s)::time" % lhs
return sql, lhs_params

models.DateTimeField.register_lookup(DateTimeTimeTransform)
}}}

We should wait for the datetime expression refactor (#25774) to land
before proceeding with this patch.

--
Ticket URL: <https://code.djangoproject.com/ticket/26348>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 11, 2016, 7:20:56 PM3/11/16
to django-...@googlegroups.com
#26348: Add a __time lookup for DateTimeField
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:1>

Django

unread,
Jun 7, 2016, 2:05:44 AM6/7/16
to django-...@googlegroups.com
#26348: Add a __time lookup for DateTimeField
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* status: new => assigned
* owner: nobody => charettes


--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:2>

Django

unread,
Jun 8, 2016, 12:57:08 AM6/8/16
to django-...@googlegroups.com
#26348: Add a __time lookup for DateTimeField
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/6744 PR] with missing Oracle
support.

--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:3>

Django

unread,
Jun 27, 2016, 7:47:00 PM6/27/16
to django-...@googlegroups.com
#26348: Add a __time lookup for DateTimeField
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


Comment:

Left a few comments for improvement on the PR.

--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:4>

Django

unread,
Jul 2, 2016, 4:32:54 PM7/2/16
to django-...@googlegroups.com
#26348: Add a __time lookup for DateTimeField
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:5>

Django

unread,
Jul 8, 2016, 12:16:18 PM7/8/16
to django-...@googlegroups.com
#26348: Add a __time lookup for DateTimeField
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:6>

Django

unread,
Jul 8, 2016, 12:36:43 PM7/8/16
to django-...@googlegroups.com
#26348: Add a __time lookup for DateTimeField
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette <charette.s@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"8a4f017f4565c51c83aabb61a816e334e8638432" 8a4f017f]:
{{{
#!CommitTicketReference repository=""
revision="8a4f017f4565c51c83aabb61a816e334e8638432"
Fixed #26348 -- Added TruncTime and exposed it through the __time lookup.

Thanks Tim for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:8>

Django

unread,
Jul 8, 2016, 12:36:43 PM7/8/16
to django-...@googlegroups.com
#26348: Add a __time lookup for DateTimeField
-------------------------------------+-------------------------------------
Reporter: charettes | Owner: charettes
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette <charette.s@…>):

In [changeset:"082c52dbedd76c312cebf3b23e04c449a94c20b6" 082c52db]:
{{{
#!CommitTicketReference repository=""
revision="082c52dbedd76c312cebf3b23e04c449a94c20b6"
Refs #25774, #26348 -- Allowed Trunc functions to operate with time
fields.

Thanks Josh for the amazing testing setup and Tim for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:7>

Reply all
Reply to author
Forward
0 new messages