Browse files

Fixed #13844 -- Avoid converting unknown db values to float

This patch removes an unconditional float(value) conversion from db
backend default convert_values() method. This can cause problems when
aggregating over character fields for example. In addition, Oracle
and SQLite already return the bare value from their convert_values().

In the long term the converting should be done by fields, and the
fields should then call database backend specific converters when
needed. The current setup is inflexible for 3rd party fields.

Thanks to Merlijn van Deen for the original patch.
  • Loading branch information...
akaariai committed Jul 15, 2012
1 parent 3434051 commit fe1e4f4e3ca3a70cdc3d327f9021b349fa75b431
Showing with 19 additions and 6 deletions.
  1. +6 −6 django/db/backends/
  2. +13 −0 tests/regressiontests/aggregation_regress/
@@ -856,19 +856,19 @@ def year_lookup_bounds_for_date_field(self, value):
return self.year_lookup_bounds(value)
def convert_values(self, value, field):
- """Coerce the value returned by the database backend into a consistent type that
- is compatible with the field type.
+ """
+ Coerce the value returned by the database backend into a consistent type
+ that is compatible with the field type.
internal_type = field.get_internal_type()
if internal_type == 'DecimalField':
return value
- elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField':
+ elif (internal_type and (internal_type.endswith('IntegerField')
+ or internal_type == 'AutoField')):
return int(value)
elif internal_type in ('DateField', 'DateTimeField', 'TimeField'):
return value
- # No field, or the field isn't known to be a decimal or integer
- # Default to a float
- return float(value)
+ return value
def check_aggregate_support(self, aggregate_func):
"""Check that the backend supports the provided aggregate
@@ -865,3 +865,16 @@ def test_filtering_by_annotation_name(self):
['Peter Norvig'],
lambda b:
+ def test_type_conversion_for_CharField_aggregations(self):
+ # regression test for #13844
+ # the database backend convert_values function should not case
+ # CharFields to float.
+ from django.db.models import CharField
+ from django.db import connection
+ testData = u'not_a_float_value'
+ testField = CharField()
+ self.assertEqual(
+ connection.ops.convert_values(testData, testField),
+ testData
+ )

0 comments on commit fe1e4f4

Please sign in to comment.