From 70c2b90d958e60c2da803502f5f20eb59ef3299a Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Fri, 12 Jul 2019 17:27:49 +0200 Subject: [PATCH] Simplified DateTimeRangeContains by making it subclass PostgresSimpleLookup. --- django/contrib/postgres/fields/ranges.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/django/contrib/postgres/fields/ranges.py b/django/contrib/postgres/fields/ranges.py index 0e8a347d5f..4aed6e6022 100644 --- a/django/contrib/postgres/fields/ranges.py +++ b/django/contrib/postgres/fields/ranges.py @@ -149,12 +149,13 @@ RangeField.register_lookup(lookups.ContainedBy) RangeField.register_lookup(lookups.Overlap) -class DateTimeRangeContains(models.Lookup): +class DateTimeRangeContains(lookups.PostgresSimpleLookup): """ Lookup for Date/DateTimeRange containment to cast the rhs to the correct type. """ lookup_name = 'contains' + operator = '@>' def process_rhs(self, compiler, connection): # Transform rhs value for db lookup. @@ -165,9 +166,7 @@ class DateTimeRangeContains(models.Lookup): return super().process_rhs(compiler, connection) def as_sql(self, compiler, connection): - lhs, lhs_params = self.process_lhs(compiler, connection) - rhs, rhs_params = self.process_rhs(compiler, connection) - params = lhs_params + rhs_params + sql, params = super().as_sql(compiler, connection) # Cast the rhs if needed. cast_sql = '' if ( @@ -178,7 +177,7 @@ class DateTimeRangeContains(models.Lookup): ): cast_internal_type = self.lhs.output_field.base_field.get_internal_type() cast_sql = '::{}'.format(connection.data_types.get(cast_internal_type)) - return '%s @> %s%s' % (lhs, rhs, cast_sql), params + return '%s%s' % (sql, cast_sql), params DateRangeField.register_lookup(DateTimeRangeContains)