Opened 5 years ago

Last modified 6 months ago

#16487 new Bug

F expression with timedelta does not work with __range query filter

Reported by: bendavis78 Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords:
Cc: bendavis78 Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no




Doesn't work:

User.objects.filter(last_login__range=(F('date_joined'), F('date_joined')+timedelta(weeks=2)))

Change History (4)

comment:1 Changed 5 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Indeed. Here's the traceback:

>>> from datetime import timedelta
>>> from django.contrib.auth.models import *
>>> from django.db.models import F
>>> User.objects.filter(last_login__gt=F('date_joined')+timedelta(weeks=2))
[<User: admin>]
>>> User.objects.filter(last_login__range=(F('date_joined'), F('date_joined')+timedelta(weeks=2)))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/", line 141, in filter
    return self.get_query_set().filter(*args, **kwargs)
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/", line 550, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/", line 568, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/sql/", line 1172, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/sql/", line 1107, in add_filter
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/sql/", line 67, in add
    value = obj.prepare(lookup_type, value)
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/sql/", line 316, in prepare
    return self.field.get_prep_lookup(lookup_type, value)
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/fields/", line 644, in get_prep_lookup
    return super(DateField, self).get_prep_lookup(lookup_type, value)
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/fields/", line 294, in get_prep_lookup
    return [self.get_prep_value(v) for v in value]
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/fields/", line 721, in get_prep_value
    return self.to_python(value)
  File "path/to/venv/lib/python2.6/site-packages/django/db/models/fields/", line 710, in to_python
    raise exceptions.ValidationError(self.error_messages['invalid'])
ValidationError: [u'Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.']

comment:2 Changed 4 years ago by anonymous

This should be supported. most databases these days either have support for time intervals or time math.

comment:3 Changed 2 years ago by bendavis78

  • Cc bendavis78 added

comment:4 Changed 6 months ago by MatthewWilkes

See also: #22288

Note: See TracTickets for help on using tickets.
Back to Top