Author: ramiro
Date: 2010-12-28 19:18:11 -0600 (Tue, 28 Dec 2010)
New Revision: 15094
Modified:
django/trunk/django/core/management/validation.py
django/trunk/docs/ref/models/fields.txt
django/trunk/tests/modeltests/invalid_models/models.py
Log:
Fixed #7726 -- Added validation of max_digits and decimal_places options to
DecimalField model field. Thanks theevilgeek for the report and elbarto for the
patch.
Modified: django/trunk/django/core/management/validation.py
===================================================================
--- django/trunk/django/core/management/validation.py 2010-12-29 00:09:46 UTC
(rev 15093)
+++ django/trunk/django/core/management/validation.py 2010-12-29 01:18:11 UTC
(rev 15094)
@@ -53,11 +53,14 @@
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.'
@@ -65,8 +68,14 @@
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):
Modified: django/trunk/docs/ref/models/fields.txt
===================================================================
--- django/trunk/docs/ref/models/fields.txt 2010-12-29 00:09:46 UTC (rev
15093)
+++ django/trunk/docs/ref/models/fields.txt 2010-12-29 01:18:11 UTC (rev
15094)
@@ -437,11 +437,12 @@
.. 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::
Modified: django/trunk/tests/modeltests/invalid_models/models.py
===================================================================
--- django/trunk/tests/modeltests/invalid_models/models.py 2010-12-29
00:09:46 UTC (rev 15093)
+++ django/trunk/tests/modeltests/invalid_models/models.py 2010-12-29
01:18:11 UTC (rev 15094)
@@ -14,6 +14,8 @@
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)])
@@ -241,6 +243,8 @@
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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.