Normalized the implementation of get_db_converters.

Put the types in the same order and checked for None consistently.
This commit is contained in:
Aymeric Augustin 2015-04-12 20:33:58 +02:00
parent ec186572e6
commit fe6b5e62b1
4 changed files with 41 additions and 36 deletions

View File

@ -505,10 +505,11 @@ class BaseDatabaseOperations(object):
return [first, second]
def get_db_converters(self, expression):
"""Get a list of functions needed to convert field data.
"""
Get a list of functions needed to convert field data.
Some field types on some backends do not provide data in the correct
format, this is the hook for coverter functions.
format, this is the hook for converter functions.
"""
return []

View File

@ -182,16 +182,21 @@ class DatabaseOperations(BaseDatabaseOperations):
def get_db_converters(self, expression):
converters = super(DatabaseOperations, self).get_db_converters(expression)
internal_type = expression.output_field.get_internal_type()
if internal_type in ['BooleanField', 'NullBooleanField']:
converters.append(self.convert_booleanfield_value)
if internal_type == 'DateTimeField':
converters.append(self.convert_datetimefield_value)
if internal_type == 'UUIDField':
converters.append(self.convert_uuidfield_value)
if internal_type == 'TextField':
converters.append(self.convert_textfield_value)
elif internal_type in ['BooleanField', 'NullBooleanField']:
converters.append(self.convert_booleanfield_value)
elif internal_type == 'DateTimeField':
converters.append(self.convert_datetimefield_value)
elif internal_type == 'UUIDField':
converters.append(self.convert_uuidfield_value)
return converters
def convert_textfield_value(self, value, expression, connection, context):
if value is not None:
value = force_text(value)
return value
def convert_booleanfield_value(self, value, expression, connection, context):
if value in (0, 1):
value = bool(value)
@ -207,8 +212,3 @@ class DatabaseOperations(BaseDatabaseOperations):
if value is not None:
value = uuid.UUID(value)
return value
def convert_textfield_value(self, value, expression, connection, context):
if value is not None:
value = force_text(value)
return value

View File

@ -174,18 +174,6 @@ WHEN (new.%(col_name)s IS NULL)
converters.append(self.convert_empty_values)
return converters
def convert_empty_values(self, value, expression, connection, context):
# Oracle stores empty strings as null. We need to undo this in
# order to adhere to the Django convention of using the empty
# string instead of null, but only if the field accepts the
# empty string.
field = expression.output_field
if value is None and field.empty_strings_allowed:
value = ''
if field.get_internal_type() == 'BinaryField':
value = b''
return value
def convert_textfield_value(self, value, expression, connection, context):
if isinstance(value, Database.LOB):
value = force_text(value.read())
@ -197,7 +185,7 @@ WHEN (new.%(col_name)s IS NULL)
return value
def convert_booleanfield_value(self, value, expression, connection, context):
if value in (1, 0):
if value in (0, 1):
value = bool(value)
return value
@ -213,7 +201,8 @@ WHEN (new.%(col_name)s IS NULL)
def convert_datefield_value(self, value, expression, connection, context):
if isinstance(value, Database.Timestamp):
return value.date()
value = value.date()
return value
def convert_timefield_value(self, value, expression, connection, context):
if isinstance(value, Database.Timestamp):
@ -225,6 +214,18 @@ WHEN (new.%(col_name)s IS NULL)
value = uuid.UUID(value)
return value
def convert_empty_values(self, value, expression, connection, context):
# Oracle stores empty strings as null. We need to undo this in
# order to adhere to the Django convention of using the empty
# string instead of null, but only if the field accepts the
# empty string.
field = expression.output_field
if value is None and field.empty_strings_allowed:
value = ''
if field.get_internal_type() == 'BinaryField':
value = b''
return value
def deferrable_sql(self):
return " DEFERRABLE INITIALLY DEFERRED"

View File

@ -151,15 +151,6 @@ class DatabaseOperations(BaseDatabaseOperations):
converters.append(self.convert_uuidfield_value)
return converters
def convert_decimalfield_value(self, value, expression, connection, context):
return backend_utils.typecast_decimal(expression.output_field.format_number(value))
def convert_datefield_value(self, value, expression, connection, context):
if value is not None:
if not isinstance(value, datetime.date):
value = parse_date(value)
return value
def convert_datetimefield_value(self, value, expression, connection, context):
if value is not None:
if not isinstance(value, datetime.datetime):
@ -168,12 +159,24 @@ class DatabaseOperations(BaseDatabaseOperations):
value = value.replace(tzinfo=timezone.utc)
return value
def convert_datefield_value(self, value, expression, connection, context):
if value is not None:
if not isinstance(value, datetime.date):
value = parse_date(value)
return value
def convert_timefield_value(self, value, expression, connection, context):
if value is not None:
if not isinstance(value, datetime.time):
value = parse_time(value)
return value
def convert_decimalfield_value(self, value, expression, connection, context):
if value is not None:
value = expression.output_field.format_number(value)
value = backend_utils.typecast_decimal(value)
return value
def convert_uuidfield_value(self, value, expression, connection, context):
if value is not None:
value = uuid.UUID(value)