Refs #22936 -- Moved more of Field.get_db_prep_lookup() to lookups.
This commit is contained in:
parent
eb5d7bc2f4
commit
eab5df12b6
|
@ -783,14 +783,7 @@ class Field(RegisterLookupMixin):
|
||||||
value = self.get_prep_lookup(lookup_type, value)
|
value = self.get_prep_lookup(lookup_type, value)
|
||||||
prepared = True
|
prepared = True
|
||||||
|
|
||||||
if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'):
|
return [value]
|
||||||
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]
|
|
||||||
|
|
||||||
def has_default(self):
|
def has_default(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -51,8 +51,7 @@ class Lookup(object):
|
||||||
sqls.append(sql)
|
sqls.append(sql)
|
||||||
sqls_params.extend(sql_params)
|
sqls_params.extend(sql_params)
|
||||||
else:
|
else:
|
||||||
params = self.lhs.output_field.get_db_prep_lookup(
|
_, params = self.get_db_prep_lookup(rhs, connection)
|
||||||
self.lookup_name, rhs, connection, prepared=True)
|
|
||||||
sqls, sqls_params = ['%s'] * len(params), params
|
sqls, sqls_params = ['%s'] * len(params), params
|
||||||
return sqls, sqls_params
|
return sqls, sqls_params
|
||||||
|
|
||||||
|
@ -164,7 +163,36 @@ class BuiltinLookup(Lookup):
|
||||||
return connection.operators[self.lookup_name] % 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'
|
lookup_name = 'exact'
|
||||||
Field.register_lookup(Exact)
|
Field.register_lookup(Exact)
|
||||||
|
|
||||||
|
@ -182,22 +210,22 @@ class IExact(BuiltinLookup):
|
||||||
Field.register_lookup(IExact)
|
Field.register_lookup(IExact)
|
||||||
|
|
||||||
|
|
||||||
class GreaterThan(BuiltinLookup):
|
class GreaterThan(FieldGetDbPrepValueMixin, BuiltinLookup):
|
||||||
lookup_name = 'gt'
|
lookup_name = 'gt'
|
||||||
Field.register_lookup(GreaterThan)
|
Field.register_lookup(GreaterThan)
|
||||||
|
|
||||||
|
|
||||||
class GreaterThanOrEqual(BuiltinLookup):
|
class GreaterThanOrEqual(FieldGetDbPrepValueMixin, BuiltinLookup):
|
||||||
lookup_name = 'gte'
|
lookup_name = 'gte'
|
||||||
Field.register_lookup(GreaterThanOrEqual)
|
Field.register_lookup(GreaterThanOrEqual)
|
||||||
|
|
||||||
|
|
||||||
class LessThan(BuiltinLookup):
|
class LessThan(FieldGetDbPrepValueMixin, BuiltinLookup):
|
||||||
lookup_name = 'lt'
|
lookup_name = 'lt'
|
||||||
Field.register_lookup(LessThan)
|
Field.register_lookup(LessThan)
|
||||||
|
|
||||||
|
|
||||||
class LessThanOrEqual(BuiltinLookup):
|
class LessThanOrEqual(FieldGetDbPrepValueMixin, BuiltinLookup):
|
||||||
lookup_name = 'lte'
|
lookup_name = 'lte'
|
||||||
Field.register_lookup(LessThanOrEqual)
|
Field.register_lookup(LessThanOrEqual)
|
||||||
|
|
||||||
|
@ -223,7 +251,7 @@ class IntegerLessThan(IntegerFieldFloatRounding, LessThan):
|
||||||
IntegerField.register_lookup(IntegerLessThan)
|
IntegerField.register_lookup(IntegerLessThan)
|
||||||
|
|
||||||
|
|
||||||
class In(BuiltinLookup):
|
class In(FieldGetDbPrepValueIterableMixin, BuiltinLookup):
|
||||||
lookup_name = 'in'
|
lookup_name = 'in'
|
||||||
|
|
||||||
def process_rhs(self, compiler, connection):
|
def process_rhs(self, compiler, connection):
|
||||||
|
@ -365,7 +393,7 @@ class IEndsWith(PatternLookup):
|
||||||
Field.register_lookup(IEndsWith)
|
Field.register_lookup(IEndsWith)
|
||||||
|
|
||||||
|
|
||||||
class Range(BuiltinLookup):
|
class Range(FieldGetDbPrepValueIterableMixin, BuiltinLookup):
|
||||||
lookup_name = 'range'
|
lookup_name = 'range'
|
||||||
|
|
||||||
def get_rhs_op(self, connection, rhs):
|
def get_rhs_op(self, connection, rhs):
|
||||||
|
|
Loading…
Reference in New Issue