Opened 16 years ago

Closed 10 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 Alex Gaynor)

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 Alex Gaynor, 16 years ago

Description: modified (diff)

Please use preview.

comment:2 by David Danier <goliath.mailinglist@…>, 16 years ago

What about day/week/....in? :)

comment:3 by Clément Nodet, 16 years ago

Owner: changed from nobody to Clément Nodet

comment:4 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:5 by anonymous, 15 years ago

+1

comment:6 by anonymous, 15 years ago

dont forget year

comment:7 by anonymous, 15 years ago

in statement will be great too

comment:8 by fas, 15 years ago

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

in reply to:  8 comment:9 by David Danier <goliath.mailinglist@…>, 15 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 Chris Beaven, 14 years ago

Severity: Normal
Type: New feature

comment:11 by Ulrich Petri, 13 years ago

Easy pickings: unset
UI/UX: unset

Related: #16187 Refactor of the lookup system

comment:12 by ANUBHAV JOSHI, 11 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 Anssi Kääriäinen, 11 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 Aymeric Augustin, 10 years ago

Resolution: wontfix
Status: newclosed

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