[Django] #26189: Models with decimal fields raise errors on calling full_clean

7 views
Skip to first unread message

Django

unread,
Feb 9, 2016, 1:58:22 AM2/9/16
to django-...@googlegroups.com
#26189: Models with decimal fields raise errors on calling full_clean
-------------------------------+--------------------
Reporter: lenniezelk | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
After assigning a value with decimal places to a decimal field in a model
, calling the model's `full_clean` method raises a `ValidationError`:
{{{#!python
----> 1 transaction.full_clean()

/home/vagrant/.virtualenvs/cashbox/lib/python3.4/site-
packages/django/db/models/base.py in full_clean(self, exclude,
validate_unique)
1134
1135 if errors:
-> 1136 raise ValidationError(errors)
1137
1138 def clean_fields(self, exclude=None):

ValidationError: {'amount': ['Ensure that there are no more than 12 digits
in total.']}
}}}

Seems to be raised by the new Decimal validator. Decimal field's
`to_python` method creates a Decimal instance that has many decimal places
which ends up violating the `max_digits` setting on the Decimal field.

--
Ticket URL: <https://code.djangoproject.com/ticket/26189>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 9, 2016, 2:00:19 AM2/9/16
to django-...@googlegroups.com
#26189: Models with decimal fields raise errors on calling full_clean
-------------------------------+--------------------------------------
Reporter: lenniezelk | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
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 lenniezelk):

* needs_docs: => 0
* type: Uncategorized => Bug
* needs_tests: => 0
* needs_better_patch: => 0


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

Django

unread,
Feb 9, 2016, 7:14:58 AM2/9/16
to django-...@googlegroups.com
#26189: Models with decimal fields raise errors on calling full_clean
-------------------------------+--------------------------------------
Reporter: lenniezelk | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by timgraham):

Can you provide your model and the code to reproduce the issue?

--
Ticket URL: <https://code.djangoproject.com/ticket/26189#comment:2>

Django

unread,
Feb 9, 2016, 8:18:59 AM2/9/16
to django-...@googlegroups.com
#26189: Models with decimal fields raise errors on calling full_clean
-------------------------------+--------------------------------------
Reporter: lenniezelk | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
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 lenniezelk):

* Attachment "models__piece.py" added.

Django

unread,
Feb 9, 2016, 8:26:00 AM2/9/16
to django-...@googlegroups.com
#26189: Models with decimal fields raise errors on calling full_clean
-------------------------------+--------------------------------------
Reporter: lenniezelk | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by lenniezelk):

I have added the relevant model in an attachment. The affected field is
`amount`. Just creating a model instance and running `full_clean` on it
raises a `ValidationError`. Error is raised even after Here is an iPython
session I ran:

{{{#!python
In [1]: user = User.objects.get(email='jer...@gmail.com')

In [2]: account = list(user.user_accounts.first())
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)
<ipython-input-2-3e7c6aabfd88> in <module>()
----> 1 account = list(user.user_accounts.first())

TypeError: 'Account' object is not iterable

In [3]: account = user.user_accounts.first()

In [4]: currency = Currency.objects.first()

In [5]: transaction = Transaction(account=account, user=user,
currency=currency, amount=34.22, date=timezone.now(),
exchange_rate=ExchangeRate.objects.first())

In [6]: transaction.full_clean()
---------------------------------------------------------------------------
ValidationError Traceback (most recent call
last)
<ipython-input-6-343b6af108cd> in <module>()
----> 1 transaction.full_clean()

/home/vagrant/.virtualenvs/cashbox/lib/python3.4/site-
packages/django/db/models/base.py in full_clean(self, exclude,
validate_unique)
1134
1135 if errors:
-> 1136 raise ValidationError(errors)
1137
1138 def clean_fields(self, exclude=None):

ValidationError: {'amount': ['Ensure that there are no more than 12 digits
in total.']}

In [7]: transaction.save()

In [8]: transaction.full_clean()
---------------------------------------------------------------------------
ValidationError Traceback (most recent call
last)
<ipython-input-8-343b6af108cd> in <module>()
----> 1 transaction.full_clean()

/home/vagrant/.virtualenvs/cashbox/lib/python3.4/site-
packages/django/db/models/base.py in full_clean(self, exclude,
validate_unique)
1134
1135 if errors:
-> 1136 raise ValidationError(errors)
1137
1138 def clean_fields(self, exclude=None):

ValidationError: {'amount_primary': ['Ensure that there are no more than
12 digits in total.'], 'amount': ['Ensure that there are no more than 12
digits in total.']}
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26189#comment:3>

Django

unread,
Feb 9, 2016, 8:36:44 AM2/9/16
to django-...@googlegroups.com
#26189: Models with decimal fields raise errors on calling full_clean
-------------------------------+--------------------------------------
Reporter: lenniezelk | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by timgraham):

Do you have the problem if you change the `default` and `amount` from a
`float` to `Decimal`, e.g. `amount=decimal.Decimal('34.22')`. This also
came up in #25417.

--
Ticket URL: <https://code.djangoproject.com/ticket/26189#comment:4>

Django

unread,
Feb 9, 2016, 9:27:46 AM2/9/16
to django-...@googlegroups.com
#26189: Models with decimal fields raise errors on calling full_clean
-------------------------------+--------------------------------------
Reporter: lenniezelk | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by lenniezelk):

Replying to [comment:4 timgraham]:


> Do you have the problem if you change the `default` and `amount` from a
`float` to `Decimal`, e.g. `amount=decimal.Decimal('34.22')`. This also
came up in #25417.

This solves the issue. Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/26189#comment:5>

Django

unread,
Feb 9, 2016, 9:29:02 AM2/9/16
to django-...@googlegroups.com
#26189: DecimalFields with float values raise errors on calling full_clean
-------------------------------------+-------------------------------------
Reporter: lenniezelk | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution: invalid
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 timgraham):

* status: new => closed
* resolution: => invalid
* component: Uncategorized => Database layer (models, ORM)


--
Ticket URL: <https://code.djangoproject.com/ticket/26189#comment:6>

Reply all
Reply to author
Forward
0 new messages