[Django] #16487: F expression with timedelta does not work with __range query filter

24 views
Skip to first unread message

Django

unread,
Jul 19, 2011, 6:55:37 PM7/19/11
to django-...@googlegroups.com
#16487: F expression with timedelta does not work with __range query filter
------------------------+----------------------------------------------
Reporter: bendavis78 | Owner: nobody
Type: Bug | Status: new
Milestone: | Component: Database layer (models, ORM)
Version: 1.3 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------+----------------------------------------------
Works:
{{{
#!python
User.objects.filter(last_login__gt=F('date_joined')+timedelta(weeks=2))
}}}

Doesn't work:
{{{
#!python
User.objects.filter(last_login__range=(F('date_joined'),
F('date_joined')+timedelta(weeks=2)))
}}}

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

Django

unread,
Jul 20, 2011, 4:28:10 AM7/20/11
to django-...@googlegroups.com
#16487: F expression with timedelta does not work with __range query filter
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
bendavis78 | Status: new
Type: Bug | Component: Database layer
Milestone: | (models, ORM)
Version: 1.3 | 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 aaugustin):

* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0


Comment:

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/manager.py", line 141, in filter
return self.get_query_set().filter(*args, **kwargs)
File "path/to/venv/lib/python2.6/site-
packages/django/db/models/query.py", line 550, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "path/to/venv/lib/python2.6/site-
packages/django/db/models/query.py", 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/query.py", 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/query.py", line 1107, in add_filter
connector)
File "path/to/venv/lib/python2.6/site-
packages/django/db/models/sql/where.py", line 67, in add
value = obj.prepare(lookup_type, value)
File "path/to/venv/lib/python2.6/site-
packages/django/db/models/sql/where.py", 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/__init__.py", 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/__init__.py", 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/__init__.py", line 721, in get_prep_value
return self.to_python(value)
File "path/to/venv/lib/python2.6/site-
packages/django/db/models/fields/__init__.py", 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.']
}}}

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

Django

unread,
Jul 26, 2012, 5:32:06 AM7/26/12
to django-...@googlegroups.com
#16487: F expression with timedelta does not work with __range query filter
-------------------------------------+-------------------------------------
Reporter: bendavis78 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by anonymous):

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

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

Django

unread,
Jun 25, 2014, 2:46:57 PM6/25/14
to django-...@googlegroups.com
#16487: F expression with timedelta does not work with __range query filter
-------------------------------------+-------------------------------------

Reporter: bendavis78 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by bendavis78):

* cc: bendavis78 (added)


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

Django

unread,
Mar 5, 2016, 4:10:39 PM3/5/16
to django-...@googlegroups.com
#16487: F expression with timedelta does not work with __range query filter
-------------------------------------+-------------------------------------

Reporter: bendavis78 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(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
-------------------------------------+-------------------------------------

Comment (by MatthewWilkes):

See also: #22288

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

Django

unread,
Sep 23, 2016, 11:11:54 AM9/23/16
to django-...@googlegroups.com
#16487: F expression with timedelta does not work with __range query filter
-------------------------------------+-------------------------------------
Reporter: Ben Davis | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 1.3
(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 Tim Graham):

* cc: Matthew Wilkes (added)


Comment:

Looks like #22288 fixed this. Matthew, do you think we should a test for
this case or are the tests from 4f138fe5a496a81115c4fba6615a517fc62c3b17
sufficient?

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

Django

unread,
Sep 24, 2016, 4:40:09 PM9/24/16
to django-...@googlegroups.com
#16487: F expression with timedelta does not work with __range query filter
-------------------------------------+-------------------------------------
Reporter: Ben Davis | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 1.3
(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
-------------------------------------+-------------------------------------

Comment (by Matthew Wilkes):

My feeling is that the current tests are sufficient, considering
https://github.com/django/django/blob/e43ea36b7681e43ea99505a2cf7550d4d36016b3/tests/expressions/tests.py#L801
includes tests about adding timedelta objects to F() expressions. The
range lookup doesn't do anything special with expressions, so anything
that works individually should work inside the lookup, as long as the SQL
generated by the lookup isn't fragile to having expressions as its
operands.

That said, I did include explicit tests for adding to the integer fields
in my PR, so obviously at some point I thought demonstrating this was
valuable, so I'd be happy to submit an amendment if you're leaning towards
explicitly checking them.

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

Django

unread,
Sep 24, 2016, 6:06:54 PM9/24/16
to django-...@googlegroups.com
#16487: F expression with timedelta does not work with __range query filter
-------------------------------------+-------------------------------------
Reporter: Ben Davis | Owner: nobody
Type: Bug | Status: closed

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

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 Tim Graham):

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


Comment:

I trust your judgment, thanks!

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

Reply all
Reply to author
Forward
0 new messages