Opened 17 years ago
Closed 11 years ago
#10911 closed New feature (wontfix)
Date comparison lookup operators
| Reported by: | vbmendes | Owned by: | Clément Nodet |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.0 |
| Severity: | Normal | Keywords: | lookup, orm, date |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
It would be nice to have lookup operators such as day_gt , day_gte , day_lt , day_lte , month_gt , month_gte , month_lt and month_lte . With this, we will be able to do comparisons such as date__month_gt=6 to find objects with the date object in the second semester, and much more good things. The lookups are:
day_lt-- day lower then;day_gt-- day greather then;day_lte-- day lower then or equal;day_gte-- day greather then or equal;day_range-- accepts a tuple or list with two elements, just like the range loopup operator;
And the same thing for month, year and week_day.
Change History (14)
comment:1 by , 17 years ago
| Description: | modified (diff) |
|---|
comment:3 by , 17 years ago
| Owner: | changed from to |
|---|
comment:4 by , 16 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
follow-up: 9 comment:8 by , 16 years ago
Soemthing for "older than 6 months" etc. would be great too.
comment:9 by , 16 years ago
Replying to fas:
Soemthing for "older than 6 months" etc. would be great too.
That's already possible:
Foo.objects.filter(datetime__lte=six_month_ago)
comment:10 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → New feature |
comment:11 by , 14 years ago
| Easy pickings: | unset |
|---|---|
| UI/UX: | unset |
Related: #16187 Refactor of the lookup system
comment:12 by , 12 years ago
Is anyone working on this or has worked in the past?
Or have been there any changes in the django version which support this.
comment:13 by , 12 years ago
These will be trivial to implement once #16187 lands.
For example month_gte could be written as:
from django.db.models import Lookup
class MonthGte(Lookup):
lookup_name = 'month_gte'
def as_sql(self, qn, connection):
lhs, lhs_params = self.process_lhs(qn, connection)
rhs, rhs_params = self.process_rhs(qn, connection)
return 'EXTRACT(month FROM %s) >= %s' % (lhs, rhs), lhs_params + rhs_params
DateField.register_lookup(MonthGte)
Or, even better way is to write a MonthExtract:
from django.db.models import Extract
class MonthExtract(Extract):
lookup_name = 'month'
output_type = IntegerField()
def as_sql(self, qn, connection):
lhs, lhs_params = qn.compile(self.lhs)
return 'EXTRACT(month FROM %s) % lhs, lhs_params
DateField.register_lookup(MonthExtract)
Using the first one implements .filter(datefield__month_gte=6) but the second way implements any lookup on month, for example both .filter(datefield__month__gte=6) and .filter(datefield__month__lte=6) will just work.
comment:14 by , 11 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
I don't think we want to provide lookups for all the permutations of (DateField, DateTimeField, TimeField) x (year, month, day, dow, doy, hour, minute, second, microsecond) x (eq, ne, gt, lt, gte, lte). So the solution given by Anssi is the way to go.
Please use preview.