15

One of my model fields is the following:

aaf_1kg_all = models.DecimalField(blank=True, null=True)

When I use my model normally, everything is fine. When I use it in a ready() hook, however, I get this error:

SystemCheckError: System check identified some issues:

ERRORS:
myapp.Model.aaf_1kg_all: (fields.E130) DecimalFields must define a 'decimal_places' attribute.
myapp.Model.aaf_1kg_all: (fields.E132) DecimalFields must define a 'max_digits' attribute.

Django docs say that these two attributes are optional. I saw this answer, but in the database both decimal places and max_digits are defined.

If I decide to add these attributes, i.e.

aaf_1kg_all = models.DecimalField(blank=True, null=True, max_digits=10, decimal_places=10)

the app runs, but at some point I get this error instead:

Traceback (most recent call last):
[...]
  variants.extend(list(sub_qs))   # sub_qs is a QuerySet
File ".../django/db/models/query.py", line 258, in __iter__
  self._fetch_all()
File ".../django/db/models/query.py", line 1074, in _fetch_all
  self._result_cache = list(self.iterator())
File ".../django/db/models/query.py", line 68, in __iter__
  for row in compiler.results_iter(results):
File ".../django/db/models/sql/compiler.py", line 808, in results_iter
  row = self.apply_converters(row, converters)
File ".../django/db/models/sql/compiler.py", line 792, in apply_converters
  value = converter(value, expression, self.connection, self.query.context)
File ".../django/db/backends/sqlite3/operations.py", line 233, in convert_decimalfield_value
  value = expression.output_field.format_number(value)
File ".../django/db/models/fields/__init__.py", line 1608, in format_number
  return utils.format_number(value, self.max_digits, self.decimal_places)
File ".../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'>]

Did I forget something ?

Community
  • 1
  • 1
JulienD
  • 7,102
  • 9
  • 50
  • 84
  • 2
    The `max_digits` and `decimal_places` arguments are optional for [form fields](https://docs.djangoproject.com/en/1.9/ref/forms/fields/#decimalfield). They are required for [model fields](https://docs.djangoproject.com/en/1.9/ref/models/fields/#decimalfield). – Alasdair Apr 19 '16 at 16:01
  • this link http://stackoverflow.com/questions/33827179/python-decimal-invalidoperation-error will help you to resolve your error – Usman Maqbool Apr 19 '16 at 16:10
  • I don't understand the downvotes, but getting an answer is all that matters. Thanks people. – JulienD Apr 19 '16 at 20:30

3 Answers3

39

Your max_digits should be greater than the decimal_places

Note that 0.1000000000 uses 10 digits, but 1.0000000000 uses 11 digits. Therefore if you have max_digits=10 and decimal_places=10, then any numbers greater or equal to 1.0 will give errors.

If you don't need so decimal places, maybe you need something like max_digits=10, decimal_places=3. Or if you really need decimal_places=10, then maybe you need something like max_digits=12, which would support values less that 1000.

Django 1.9 has added a validator to try to prevent errors like this. See ticket 24636 for more details.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • If it's true that `max_digits` must be greater than `decimal_places`, then that's a documentation bug. According to [the documentation](https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.DecimalField.max_digits), `Note that this number must be greater than or equal to decimal_places.` – Joey Wilhelm Apr 19 '16 at 16:24
  • 4
    The documentation is correct, they can be the same. However if they are the same, then the field cannot handle values greater to or equal to `1.0`. – Alasdair Apr 19 '16 at 16:27
9

Argument max_digits must be greater then decimal_places.

e.g

aaf_1kg_all = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
2

DECIMAL columns do not permit values larger than the range implied by the column definition. For example, a DECIMAL(3,0) column supports a range of -999 to 999. A DECIMAL(M, D) column permits up to M - D digits to the left of the decimal point.

Mysql Documentation

If you want to accept 10 digits in both number and decimal positions you need to declare 'aaf_1kg_all' like this decimal_places=10)

aaf_1kg_all = models.DecimalField(blank=True, null=True, max_digits=20,  decimal_places=10)
kiran sai
  • 37
  • 1
  • 6