Opened 7 years ago

Closed 7 years ago

#28851 closed New feature (needsinfo)

Support Open Ranges for Range Lookup - convert None to 'infinity'

Reported by: Sven R. Kunze Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi Dev Team,

we recently came across the following issue. We wanted to search for an open ranges using the range lookup.

Model.objects.filter(date__range=(None, now()))
Model.objects.filter(date__range=(now(), None))

Right now, we monkey-patch Django like this:

from django.db.models.lookups import Range

class OpenRange(Range):
    lookup_name = 'range'

    def as_sql(self, compiler, connection):
        lhs, lhs_params = self.process_lhs(compiler, connection)
        rhs, rhs_params = self.process_rhs(compiler, connection)
        if not rhs_params[0]:
            rhs_params[0] = '-infinity'
        if not rhs_params[1]:
            rhs_params[1] = 'infinity'
        return "%s BETWEEN %s AND %s" % (lhs, rhs[0], rhs[1]), rhs_params

models.DateField.register_lookup(OpenRange)

Cheers,
Sven

Change History (2)

comment:1 by Simon Charette, 7 years ago

Any reason why you can't use date__gte and date__lte?

In order to accept this we'd have to make sure all the supported database engines are able to use open ranges (is infity supported for all datatypes?) and whether or not these engines will be smart enough to use an index if this value is used. For these reasons I'm -0 on adding this as it doesn't seem to bring benefits over the existing lt, gt and friends lookups.

comment:2 by Tim Graham, 7 years ago

Resolution: needsinfo
Status: newclosed

Feel free to reopen if Simon's concerns are incorrect.

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