Permalink
Browse files

Refs #22936 -- Moved more of Field.get_db_prep_lookup() to lookups.

  • Loading branch information...
timgraham committed Apr 22, 2016
1 parent eb5d7bc commit eab5df12b664b154b2e280330aa43d8c0621b94a
Showing with 38 additions and 17 deletions.
  1. +1 −8 django/db/models/fields/__init__.py
  2. +37 −9 django/db/models/lookups.py
@@ -783,14 +783,7 @@ def get_db_prep_lookup(self, lookup_type, value, connection,
value = self.get_prep_lookup(lookup_type, value)
prepared = True
- if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'):
- return [self.get_db_prep_value(value, connection=connection,
- prepared=prepared)]
- elif lookup_type in ('range', 'in'):
- return [self.get_db_prep_value(v, connection=connection,
- prepared=prepared) for v in value]
- else:
- return [value]
+ return [value]
def has_default(self):
"""
@@ -51,8 +51,7 @@ def batch_process_rhs(self, compiler, connection, rhs=None):
sqls.append(sql)
sqls_params.extend(sql_params)
else:
- params = self.lhs.output_field.get_db_prep_lookup(
- self.lookup_name, rhs, connection, prepared=True)
+ _, params = self.get_db_prep_lookup(rhs, connection)
sqls, sqls_params = ['%s'] * len(params), params
return sqls, sqls_params
@@ -164,7 +163,36 @@ def get_rhs_op(self, connection, rhs):
return connection.operators[self.lookup_name] % rhs
-class Exact(BuiltinLookup):
+class FieldGetDbPrepValueMixin(object):
+ """
+ Some lookups require Field.get_db_prep_value() to be called on their
+ inputs.
+ """
+ get_db_prep_lookup_value_is_iterable = False
+
+ def get_db_prep_lookup(self, value, connection):
+ # For relational fields, use the output_field of the 'field' attribute.
+ field = getattr(self.lhs.output_field, 'field', None)
+ get_db_prep_value = getattr(field, 'get_db_prep_value', None)
+ if not get_db_prep_value:
+ get_db_prep_value = self.lhs.output_field.get_db_prep_value
+ return (
+ '%s',
+ [get_db_prep_value(v, connection, prepared=True) for v in value]
+ if self.get_db_prep_lookup_value_is_iterable else
+ [get_db_prep_value(value, connection, prepared=True)]
+ )
+
+
+class FieldGetDbPrepValueIterableMixin(FieldGetDbPrepValueMixin):
+ """
+ Some lookups require Field.get_db_prep_value() to be called on each value
+ in an iterable.
+ """
+ get_db_prep_lookup_value_is_iterable = True
+
+
+class Exact(FieldGetDbPrepValueMixin, BuiltinLookup):
lookup_name = 'exact'
Field.register_lookup(Exact)
@@ -182,22 +210,22 @@ def process_rhs(self, qn, connection):
Field.register_lookup(IExact)
-class GreaterThan(BuiltinLookup):
+class GreaterThan(FieldGetDbPrepValueMixin, BuiltinLookup):
lookup_name = 'gt'
Field.register_lookup(GreaterThan)
-class GreaterThanOrEqual(BuiltinLookup):
+class GreaterThanOrEqual(FieldGetDbPrepValueMixin, BuiltinLookup):
lookup_name = 'gte'
Field.register_lookup(GreaterThanOrEqual)
-class LessThan(BuiltinLookup):
+class LessThan(FieldGetDbPrepValueMixin, BuiltinLookup):
lookup_name = 'lt'
Field.register_lookup(LessThan)
-class LessThanOrEqual(BuiltinLookup):
+class LessThanOrEqual(FieldGetDbPrepValueMixin, BuiltinLookup):
lookup_name = 'lte'
Field.register_lookup(LessThanOrEqual)
@@ -223,7 +251,7 @@ class IntegerLessThan(IntegerFieldFloatRounding, LessThan):
IntegerField.register_lookup(IntegerLessThan)
-class In(BuiltinLookup):
+class In(FieldGetDbPrepValueIterableMixin, BuiltinLookup):
lookup_name = 'in'
def process_rhs(self, compiler, connection):
@@ -365,7 +393,7 @@ def process_rhs(self, qn, connection):
Field.register_lookup(IEndsWith)
-class Range(BuiltinLookup):
+class Range(FieldGetDbPrepValueIterableMixin, BuiltinLookup):
lookup_name = 'range'
def get_rhs_op(self, connection, rhs):

0 comments on commit eab5df1

Please sign in to comment.