#36233: Specific DecimalField with max_digits > 15 can be stored but not
retrieved
on SQLite
-------------------------------------+-------------------------------------
Reporter: Orazio | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: sqlite, orm, | Triage Stage: Accepted
decimalfield, invalidoperation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):
I'm not sure if this relates to a particular version of Python or SQLite
(I tested with Python 3.12.3 and `sqlite3.sqlite_version` of 3.46) and if
it now implements the `decimal` data type differently but it appears to be
returning an `int` instance when there are no floating values and in this
case the quantization is completely unnecessary. Here's a demonstration of
how the code could be adapted to take that into consideration.
{{{#!diff
diff --git a/django/db/backends/sqlite3/operations.py
b/django/db/backends/sqlite3/operations.py
index 08de246d70..9ad0bf3833 100644
--- a/django/db/backends/sqlite3/operations.py
+++ b/django/db/backends/sqlite3/operations.py
@@ -342,7 +342,9 @@ def get_decimalfield_converter(self, expression):
)
def converter(value, expression, connection):
- if value is not None:
+ if isinstance(value, int):
+ return decimal.Decimal(value)
+ elif value is not None:
return create_decimal(value).quantize(
quantize_value,
context=expression.output_field.context
)
@@ -350,7 +352,9 @@ def converter(value, expression, connection):
else:
def converter(value, expression, connection):
- if value is not None:
+ if isinstance(value, int):
+ return decimal.Decimal(value)
+ elif value is not None:
return create_decimal(value)
return converter
}}}
Note that I haven't put much thoughts into it but it seemed adequate to
only apply float quantization when provided `float` instances?
--
Ticket URL: <https://code.djangoproject.com/ticket/36233#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/010701956d2379f1-f8716efe-e901-47e2-bfb6-cace8966f12d-000000%40eu-central-1.amazonses.com.