From 858cfd74e958b63e81657e5a9fc5f751c19e8192 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Sat, 13 Jul 2019 10:55:19 +0200 Subject: [PATCH] Simplified RangeContainedBy by making it subclass PostgresSimpleLookup. --- django/contrib/postgres/fields/ranges.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/django/contrib/postgres/fields/ranges.py b/django/contrib/postgres/fields/ranges.py index 4aed6e6022..9a8932bcee 100644 --- a/django/contrib/postgres/fields/ranges.py +++ b/django/contrib/postgres/fields/ranges.py @@ -184,7 +184,7 @@ DateRangeField.register_lookup(DateTimeRangeContains) DateTimeRangeField.register_lookup(DateTimeRangeContains) -class RangeContainedBy(models.Lookup): +class RangeContainedBy(lookups.PostgresSimpleLookup): lookup_name = 'contained_by' type_mapping = { 'integer': 'int4range', @@ -193,17 +193,18 @@ class RangeContainedBy(models.Lookup): 'date': 'daterange', 'timestamp with time zone': 'tstzrange', } + operator = '<@' - def as_sql(self, qn, connection): - field = self.lhs.output_field - if isinstance(field, models.FloatField): - sql = '%s::numeric <@ %s::{}'.format(self.type_mapping[field.db_type(connection)]) - else: - sql = '%s <@ %s::{}'.format(self.type_mapping[field.db_type(connection)]) - lhs, lhs_params = self.process_lhs(qn, connection) - rhs, rhs_params = self.process_rhs(qn, connection) - params = lhs_params + rhs_params - return sql % (lhs, rhs), params + def process_rhs(self, compiler, connection): + rhs, rhs_params = super().process_rhs(compiler, connection) + cast_type = self.type_mapping[self.lhs.output_field.db_type(connection)] + return '%s::%s' % (rhs, cast_type), rhs_params + + def process_lhs(self, compiler, connection): + lhs, lhs_params = super().process_lhs(compiler, connection) + if isinstance(self.lhs.output_field, models.FloatField): + lhs = '%s::numeric' % lhs + return lhs, lhs_params def get_prep_lookup(self): return RangeField().get_prep_value(self.rhs)