Opened 7 years ago

Closed 2 years 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/....in? :)

comment:3 Changed 7 years ago by clement

  • Owner changed from nobody to clement

comment:4 Changed 7 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 7 years ago by anonymous

+1

comment:6 Changed 7 years ago by anonymous

dont forget year

comment:7 Changed 7 years ago by anonymous

in statement will be great too

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

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

comment:9 in reply to: ↑ 8 Changed 7 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:

 Foo.objects.filter(datetime__lte=six_month_ago)

comment:10 Changed 5 years ago by SmileyChris

  • Severity set to Normal
  • Type set to New feature

comment:11 Changed 5 years ago by UloPe

  • Easy pickings unset
  • UI/UX unset

Related: #16187 Refactor of the lookup system

comment:12 Changed 2 years 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 2 years 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
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 Changed 2 years 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