#24793: Subtracting DateTime fields in a query expression should use timediff
-------------------------------------+-------------------------------------
     Reporter:  sparkyb              |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  datetime duration    |             Triage Stage:  Accepted
  expression                         |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by jarshwah):

 * cc: josh.smeaton@… (added)
 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0
 * type:  Uncategorized => New feature
 * stage:  Unreviewed => Accepted


Comment:

 Backends that don't have native duration types (mysql and sqlite) do not
 have support for `date - date` annotations. This is guarded by the
 `has_native_duration_field` flag in tests.

 You can implement your own expression that uses timediff though:

 {{{

 class TimeDiff(Func):
     function = 'timediff'
     output_field = TimeField()
     # you could also implement __init__ to enforce only two date fields

 Event.objects.annotate(duration=TimeDiff('end_time', 'start_time')

 }}}

 Note that I've made the output_field a TimeField rather than a
 DurationField. MySQL returns Time as the result of `date-date`, not a
 duration/interval.

 Considering that adding/subtracting dates is relatively common, I wouldn't
 mind seeing better support for mysql and sqlite if it's possible. So, I'm
 accepting on that basis. If someone is able to provide a patch that makes
 `F('date') - F('otherdate')` work consistently on all backends, then we'd
 probably accept that.

--
Ticket URL: <https://code.djangoproject.com/ticket/24793#comment:1>
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/065.b4cfabef4e1f631316c64ab482d81603%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to