Author: ramiro
Date: 2010-12-28 19:26:45 -0600 (Tue, 28 Dec 2010)
New Revision: 15095

Modified:
   django/branches/releases/1.2.X/django/core/management/validation.py
   django/branches/releases/1.2.X/docs/ref/models/fields.txt
   django/branches/releases/1.2.X/tests/modeltests/invalid_models/models.py
Log:
[1.2.X] 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.

Backport of [15094] from trunk.

Modified: django/branches/releases/1.2.X/django/core/management/validation.py
===================================================================
--- django/branches/releases/1.2.X/django/core/management/validation.py 
2010-12-29 01:18:11 UTC (rev 15094)
+++ django/branches/releases/1.2.X/django/core/management/validation.py 
2010-12-29 01:26:45 UTC (rev 15095)
@@ -52,11 +52,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.'
@@ -64,8 +67,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/branches/releases/1.2.X/docs/ref/models/fields.txt
===================================================================
--- django/branches/releases/1.2.X/docs/ref/models/fields.txt   2010-12-29 
01:18:11 UTC (rev 15094)
+++ django/branches/releases/1.2.X/docs/ref/models/fields.txt   2010-12-29 
01:26:45 UTC (rev 15095)
@@ -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/branches/releases/1.2.X/tests/modeltests/invalid_models/models.py
===================================================================
--- django/branches/releases/1.2.X/tests/modeltests/invalid_models/models.py    
2010-12-29 01:18:11 UTC (rev 15094)
+++ django/branches/releases/1.2.X/tests/modeltests/invalid_models/models.py    
2010-12-29 01:26:45 UTC (rev 15095)
@@ -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)])
@@ -235,6 +237,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 django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to