Simplified RangeContainedBy by making it subclass PostgresSimpleLookup.

This commit is contained in:
Mariusz Felisiak 2019-07-13 10:55:19 +02:00 committed by GitHub
parent 70c2b90d95
commit 858cfd74e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 11 deletions

View File

@ -184,7 +184,7 @@ DateRangeField.register_lookup(DateTimeRangeContains)
DateTimeRangeField.register_lookup(DateTimeRangeContains) DateTimeRangeField.register_lookup(DateTimeRangeContains)
class RangeContainedBy(models.Lookup): class RangeContainedBy(lookups.PostgresSimpleLookup):
lookup_name = 'contained_by' lookup_name = 'contained_by'
type_mapping = { type_mapping = {
'integer': 'int4range', 'integer': 'int4range',
@ -193,17 +193,18 @@ class RangeContainedBy(models.Lookup):
'date': 'daterange', 'date': 'daterange',
'timestamp with time zone': 'tstzrange', 'timestamp with time zone': 'tstzrange',
} }
operator = '<@'
def as_sql(self, qn, connection): def process_rhs(self, compiler, connection):
field = self.lhs.output_field rhs, rhs_params = super().process_rhs(compiler, connection)
if isinstance(field, models.FloatField): cast_type = self.type_mapping[self.lhs.output_field.db_type(connection)]
sql = '%s::numeric <@ %s::{}'.format(self.type_mapping[field.db_type(connection)]) return '%s::%s' % (rhs, cast_type), rhs_params
else:
sql = '%s <@ %s::{}'.format(self.type_mapping[field.db_type(connection)]) def process_lhs(self, compiler, connection):
lhs, lhs_params = self.process_lhs(qn, connection) lhs, lhs_params = super().process_lhs(compiler, connection)
rhs, rhs_params = self.process_rhs(qn, connection) if isinstance(self.lhs.output_field, models.FloatField):
params = lhs_params + rhs_params lhs = '%s::numeric' % lhs
return sql % (lhs, rhs), params return lhs, lhs_params
def get_prep_lookup(self): def get_prep_lookup(self):
return RangeField().get_prep_value(self.rhs) return RangeField().get_prep_value(self.rhs)