#32761: Model meta field "to_python" behaviour with decimal fields
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
aiacamposm |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When calling {{{to_python}}} method of a model field I expect it returns
the exact same value that will be stored in the database, i.e., when saved
the instance and retrieved later, to have this same value as the
corresponding attribute. However, it doesn't seem to behave correctly when
the field is of type DecimalField.
I have a model {{{MyModel}}} that has a field {{{foo}}} with
{{{max_digits=10}}} and {{{decimal_places=2}}}, so when I save a value
3.4211 (passed as a common float) the value stored is Decimal("3.42").
However, if call {{{MyModel._meta.get_field("foo").to_python(3.4211)}}}
the result is Decimal("3.421100000").
Inspecting the source code of this method {{{to_python}}} I guess the
problem is that it uses decimal.Context with {{{prec}}} equal to the
number of {{{max_digits}}}, which is something that only makes sense if
the number passed has {{{max_digits}}} meaningful digits.
Should it be changed to return the decimal with the specified number of
digits, or am I wrong assuming that {{{to_python}}} should return the
exact same value that will be stored?
--
Ticket URL: <https://code.djangoproject.com/ticket/32761>
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 on the web visit
https://groups.google.com/d/msgid/django-updates/053.3b15699afd512d3678404909e560cc64%40djangoproject.com.