[Django] #32761: Model meta field "to_python" behaviour with decimal fields

9 views
Skip to first unread message

Django

unread,
May 18, 2021, 6:32:30 PM5/18/21
to django-...@googlegroups.com
#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.

Django

unread,
May 18, 2021, 9:08:21 PM5/18/21
to django-...@googlegroups.com
#32761: Model meta field "to_python" behaviour with decimal fields
-------------------------------------+-------------------------------------
Reporter: aiacamposm | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #26459.

--
Ticket URL: <https://code.djangoproject.com/ticket/32761#comment:1>

Reply all
Reply to author
Forward
0 new messages