Ticket #16015: 16015_DecimalField_invalid_defaults.diff

File 16015_DecimalField_invalid_defaults.diff, 2.9 KB (added by ShawnMilo, 13 years ago)

Decimal validation patch, DEMO ONLY -- do not apply, pending design decision

  • django/core/management/validation.py

    diff --git django/core/management/validation.py django/core/management/validation.py
    index bb4fa55..83013a4 100644
     
    11import sys
     2from decimal import Decimal
    23
    34from django.contrib.contenttypes.generic import GenericForeignKey, GenericRelation
    45from django.core.management.color import color_style
    def get_validation_errors(outfile, app=None):  
    6869                except (ValueError, TypeError):
    6970                    e.add(opts, mdigits_msg % f.name)
    7071                invalid_values_msg = '"%s": DecimalFields require a "max_digits" attribute value that is greater than the value of the "decimal_places" attribute.'
     72                default_type_msg ='"%s": DecimalFields require a "default" attribute that is of type Decimal.'
     73                if f.has_default():
     74                    try:
     75                        Decimal(f.default)
     76                    except (ValueError, TypeError):
     77                        e.add(opts,  default_type_msg % f.name)
    7178                if decimalp_ok and mdigits_ok:
    7279                    if decimal_places >= max_digits:
    7380                        e.add(opts, invalid_values_msg % f.name)
  • tests/modeltests/invalid_models/models.py

    diff --git tests/modeltests/invalid_models/models.py tests/modeltests/invalid_models/models.py
    index ab9edd6..3d81c3f 100644
    class FieldErrors(models.Model):  
    1616    decimalfield3 = models.DecimalField(max_digits="bad", decimal_places="bad")
    1717    decimalfield4 = models.DecimalField(max_digits=9, decimal_places=10)
    1818    decimalfield5 = models.DecimalField(max_digits=10, decimal_places=10)
     19    decimalfield6 = models.DecimalField(max_digits=10, decimal_places=2, default = 2.34)
    1920    filefield = models.FileField()
    2021    choices = models.CharField(max_length=10, choices='bad')
    2122    choices2 = models.CharField(max_length=10, choices=[(1,2,3),(1,2,3)])
    invalid_models.fielderrors: "decimalfield3": DecimalFields require a "decimal_pl  
    245246invalid_models.fielderrors: "decimalfield3": DecimalFields require a "max_digits" attribute that is a positive integer.
    246247invalid_models.fielderrors: "decimalfield4": DecimalFields require a "max_digits" attribute value that is greater than the value of the "decimal_places" attribute.
    247248invalid_models.fielderrors: "decimalfield5": DecimalFields require a "max_digits" attribute value that is greater than the value of the "decimal_places" attribute.
     249invalid_models.fielderrors: "decimalfield6": DecimalFields require a "default" attribute that is of type Decimal.
    248250invalid_models.fielderrors: "filefield": FileFields require an "upload_to" attribute.
    249251invalid_models.fielderrors: "choices": "choices" should be iterable (e.g., a tuple or list).
    250252invalid_models.fielderrors: "choices2": "choices" should be a sequence of two-tuples.
Back to Top