#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.7fc9afc24634b79fd59ae7c14fb103b6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to