Ticket #12404: 12404_patch.diff

File 12404_patch.diff, 1.5 KB (added by josh, 15 years ago)

Add CharField max_length validation to mange.py 'validate' command.

  • django/core/management/validation.py

     
    3737                e.add(opts, '"%s": You can\'t use "id" as a field name, because each model automatically gets an "id" field if none of the fields have primary_key=True. You need to either remove/rename your "id" field or add primary_key=True to a field.' % f.name)
    3838            if f.name.endswith('_'):
    3939                e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name)
    40             if isinstance(f, models.CharField) and f.max_length in (None, 0):
    41                 e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name)
     40            if isinstance(f, models.CharField):
     41                try:
     42                    max_length = int(f.max_length)
     43                    if max_length <= 0:
     44                        e.add(opts, '"%s": CharFields require a "max_length attribute greater than zero."' % f.name)
     45                except ValueError:
     46                    e.add(opts, '"%s": CharFields require a "max_length" attribute of type int.' % f.name)
    4247            if isinstance(f, models.DecimalField):
    4348                if f.decimal_places is None:
    4449                    e.add(opts, '"%s": DecimalFields require a "decimal_places" attribute.' % f.name)
Back to Top