Skip to content

Commit

Permalink
Fixed #7726 -- Added validation of max_digits and decimal_places opti…
Browse files Browse the repository at this point in the history
…ons to DecimalField model field. Thanks theevilgeek for the report and elbarto for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15094 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Dec 29, 2010
1 parent 1517659 commit a43c2f5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
9 changes: 9 additions & 0 deletions django/core/management/validation.py
Expand Up @@ -53,20 +53,29 @@ def get_validation_errors(outfile, app=None):
except (ValueError, TypeError):
e.add(opts, '"%s": CharFields require a "max_length" attribute that is a positive integer.' % f.name)
if isinstance(f, models.DecimalField):
decimalp_ok, mdigits_ok = False, False
decimalp_msg ='"%s": DecimalFields require a "decimal_places" attribute that is a non-negative integer.'
try:
decimal_places = int(f.decimal_places)
if decimal_places < 0:
e.add(opts, decimalp_msg % f.name)
else:
decimalp_ok = True
except (ValueError, TypeError):
e.add(opts, decimalp_msg % f.name)
mdigits_msg = '"%s": DecimalFields require a "max_digits" attribute that is a positive integer.'
try:
max_digits = int(f.max_digits)
if max_digits <= 0:
e.add(opts, mdigits_msg % f.name)
else:
mdigits_ok = True
except (ValueError, TypeError):
e.add(opts, mdigits_msg % f.name)
invalid_values_msg = '"%s": DecimalFields require a "max_digits" attribute value that is greater than the value of the "decimal_places" attribute.'
if decimalp_ok and mdigits_ok:
if decimal_places >= max_digits:
e.add(opts, invalid_values_msg % f.name)
if isinstance(f, models.FileField) and not f.upload_to:
e.add(opts, '"%s": FileFields require an "upload_to" attribute.' % f.name)
if isinstance(f, models.ImageField):
Expand Down
5 changes: 3 additions & 2 deletions docs/ref/models/fields.txt
Expand Up @@ -437,11 +437,12 @@ A fixed-precision decimal number, represented in Python by a

.. attribute:: DecimalField.max_digits

The maximum number of digits allowed in the number
The maximum number of digits allowed in the number. Note that this number
must be greater than ``decimal_places``, if it exists.

.. attribute:: DecimalField.decimal_places

The number of decimal places to store with the number
The number of decimal places to store with the number.

For example, to store numbers up to 999 with a resolution of 2 decimal places,
you'd use::
Expand Down
4 changes: 4 additions & 0 deletions tests/modeltests/invalid_models/models.py
Expand Up @@ -14,6 +14,8 @@ class FieldErrors(models.Model):
decimalfield = models.DecimalField()
decimalfield2 = models.DecimalField(max_digits=-1, decimal_places=-1)
decimalfield3 = models.DecimalField(max_digits="bad", decimal_places="bad")
decimalfield4 = models.DecimalField(max_digits=9, decimal_places=10)
decimalfield5 = models.DecimalField(max_digits=10, decimal_places=10)
filefield = models.FileField()
choices = models.CharField(max_length=10, choices='bad')
choices2 = models.CharField(max_length=10, choices=[(1,2,3),(1,2,3)])
Expand Down Expand Up @@ -241,6 +243,8 @@ class ArticleAttachment(models.Model):
invalid_models.fielderrors: "decimalfield2": DecimalFields require a "max_digits" attribute that is a positive integer.
invalid_models.fielderrors: "decimalfield3": DecimalFields require a "decimal_places" attribute that is a non-negative integer.
invalid_models.fielderrors: "decimalfield3": DecimalFields require a "max_digits" attribute that is a positive integer.
invalid_models.fielderrors: "decimalfield4": DecimalFields require a "max_digits" attribute value that is greater than the value of the "decimal_places" attribute.
invalid_models.fielderrors: "decimalfield5": DecimalFields require a "max_digits" attribute value that is greater than the value of the "decimal_places" attribute.
invalid_models.fielderrors: "filefield": FileFields require an "upload_to" attribute.
invalid_models.fielderrors: "choices": "choices" should be iterable (e.g., a tuple or list).
invalid_models.fielderrors: "choices2": "choices" should be a sequence of two-tuples.
Expand Down

0 comments on commit a43c2f5

Please sign in to comment.