Opened 13 years ago
Closed 8 years ago
#16487 closed Bug (fixed)
F expression with timedelta does not work with __range query filter
Reported by: | Ben Davis | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | Ben Davis, Matthew Wilkes | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Works:
User.objects.filter(last_login__gt=F('date_joined')+timedelta(weeks=2))
Doesn't work:
User.objects.filter(last_login__range=(F('date_joined'), F('date_joined')+timedelta(weeks=2)))
Change History (7)
comment:1 by , 13 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 12 years ago
This should be supported. most databases these days either have support for time intervals or time math.
comment:3 by , 11 years ago
Cc: | added |
---|
comment:5 by , 8 years ago
Cc: | added |
---|
Looks like #22288 fixed this. Matthew, do you think we should a test for this case or are the tests from 4f138fe5a496a81115c4fba6615a517fc62c3b17 sufficient?
comment:6 by , 8 years ago
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.
Indeed. Here's the traceback: