#24636: InvalidOperation saving decimal.Decimal
-------------------------------------+-------------------------------------
     Reporter:  glic3rinu            |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  InvalidOperation     |             Triage Stage:
  decimal Decimal                    |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by glic3rinu):

 {{{
 class InvalidOperationModel(models.Model):
     value = models.DecimalField(max_digits=2, decimal_places=2)
 }}}

 {{{
 >>> a = InvalidOperationModel(value=1)
 >>> a.full_clean()
 >>> a.save()
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py",
 line 710, in save
     force_update=force_update, update_fields=update_fields)
   File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py",
 line 738, in save_base
     updated = self._save_table(raw, cls, force_insert, force_update,
 using, update_fields)
   File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py",
 line 822, in _save_table
     result = self._do_insert(cls._base_manager, using, fields, update_pk,
 raw)
   File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py",
 line 861, in _do_insert
     using=using, raw=raw)
   File "/usr/local/lib/python3.4/dist-
 packages/django/db/models/manager.py", line 127, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py",
 line 920, in _insert
     return query.get_compiler(using=using).execute_sql(return_id)
   File "/usr/local/lib/python3.4/dist-
 packages/django/db/models/sql/compiler.py", line 962, in execute_sql
     for sql, params in self.as_sql():
   File "/usr/local/lib/python3.4/dist-
 packages/django/db/models/sql/compiler.py", line 920, in as_sql
     for obj in self.query.objs
   File "/usr/local/lib/python3.4/dist-
 packages/django/db/models/sql/compiler.py", line 920, in <listcomp>
     for obj in self.query.objs
   File "/usr/local/lib/python3.4/dist-
 packages/django/db/models/sql/compiler.py", line 918, in <listcomp>
     ) for f in fields
   File "/usr/local/lib/python3.4/dist-
 packages/django/db/models/fields/__init__.py", line 1627, in
 get_db_prep_save
     self.max_digits, self.decimal_places)
   File "/usr/local/lib/python3.4/dist-
 packages/django/db/backends/base/operations.py", line 477, in
 value_to_db_decimal
     return utils.format_number(value, max_digits, decimal_places)
   File "/usr/local/lib/python3.4/dist-
 packages/django/db/backends/utils.py", line 200, in format_number
     value = value.quantize(decimal.Decimal(".1") ** decimal_places,
 context=context)
 decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
 }}}

 with `max_digits=3` works for `value=1`, but fails for `value=11`.

 the documentation says that `max_digits` must be "greater than or equal to
 decimal_places''.

 
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.DecimalField.max_digits

 I believe this could be a regression introduced in 1.8, since my code
 appeared to work in 1.7.

--
Ticket URL: <https://code.djangoproject.com/ticket/24636#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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.5709f8959a499a0b6d38f2b51e870d33%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to