diff options
| author | Sergey Fedoseev <fedoseev.sergey@gmail.com> | 2017-12-27 04:12:37 +0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-12-26 18:12:37 -0500 |
| commit | d0f569b350bca89eeb186523d8905a6e31b5a947 (patch) | |
| tree | 14d05e7c7b14b18422e59560f4964bd67570b1dd /django/db/backends/sqlite3/operations.py | |
| parent | 4c599ece57fa009cf3615f09497f81bfa6a585a7 (diff) | |
Refs #28459 -- Improved performance of loading DecimalField on SQLite.
Diffstat (limited to 'django/db/backends/sqlite3/operations.py')
| -rw-r--r-- | django/db/backends/sqlite3/operations.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py index b58c344e6b..69744365a2 100644 --- a/django/db/backends/sqlite3/operations.py +++ b/django/db/backends/sqlite3/operations.py @@ -214,7 +214,7 @@ class DatabaseOperations(BaseDatabaseOperations): elif internal_type == 'TimeField': converters.append(self.convert_timefield_value) elif internal_type == 'DecimalField': - converters.append(self.convert_decimalfield_value) + converters.append(self.get_decimalfield_converter(expression)) elif internal_type == 'UUIDField': converters.append(self.convert_uuidfield_value) elif internal_type in ('NullBooleanField', 'BooleanField'): @@ -241,15 +241,21 @@ class DatabaseOperations(BaseDatabaseOperations): value = parse_time(value) return value - def convert_decimalfield_value(self, value, expression, connection): - if value is not None: - if not isinstance(expression, Col): - # SQLite stores only 15 significant digits. Digits coming from - # float inaccuracy must be removed. - return decimal.Context(prec=15).create_decimal_from_float(value) - value = expression.output_field.format_number(value) - return decimal.Decimal(value) - return value + def get_decimalfield_converter(self, expression): + # SQLite stores only 15 significant digits. Digits coming from + # float inaccuracy must be removed. + create_decimal = decimal.Context(prec=15).create_decimal_from_float + if isinstance(expression, Col): + quantize_value = decimal.Decimal(1).scaleb(-expression.output_field.decimal_places) + + def converter(value, expression, connection): + if value is not None: + return create_decimal(value).quantize(quantize_value, context=expression.output_field.context) + else: + def converter(value, expression, connection): + if value is not None: + return create_decimal(value) + return converter def convert_uuidfield_value(self, value, expression, connection): if value is not None: |
