Opened 7 years ago

Closed 18 months ago

#10911 closed New feature (wontfix)

Date comparison lookup operators

Reported by: vbmendes Owned by: clement
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 Alex)

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 Changed 7 years ago by Alex

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Please use preview.

comment:2 Changed 7 years ago by David Danier <goliath.mailinglist@…>

What about day/week/ :)

comment:3 Changed 7 years ago by clement

  • Owner changed from nobody to clement

comment:4 Changed 6 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 6 years ago by anonymous


comment:6 Changed 6 years ago by anonymous

dont forget year

comment:7 Changed 6 years ago by anonymous

in statement will be great too

comment:8 follow-up: Changed 6 years ago by fas

Soemthing for "older than 6 months" etc. would be great too.

comment:9 in reply to: ↑ 8 Changed 6 years ago by David Danier <goliath.mailinglist@…>

Replying to fas:

Soemthing for "older than 6 months" etc. would be great too.

That's already possible:


comment:10 Changed 5 years ago by SmileyChris

  • Severity set to Normal
  • Type set to New feature

comment:11 Changed 4 years ago by UloPe

  • Easy pickings unset
  • UI/UX unset

Related: #16187 Refactor of the lookup system

comment:12 Changed 23 months ago by anubhav9042

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 Changed 23 months ago by akaariai

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

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

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 Changed 18 months ago by aaugustin

  • Resolution set to wontfix
  • Status changed from new to 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.

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