Ticket #12404: 12404_new.diff

File 12404_new.diff, 2.9 KB (added by Leo Shklovskii, 14 years ago)

updated patch with tests

  • 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)
     47                except TypeError:
     48                    if f.max_length is None:
     49                        e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name)
     50                    else:
     51                        e.add(opts, '"%s": CharFields require a "max_length" attribute of type int.' % f.name)
     52                   
    4253            if isinstance(f, models.DecimalField):
    4354                if f.decimal_places is None:
    4455                    e.add(opts, '"%s": DecimalFields require a "decimal_places" attribute.' % f.name)
  • tests/modeltests/invalid_models/models.py

     
    88
    99class FieldErrors(models.Model):
    1010    charfield = models.CharField()
     11    badmax = models.CharField(max_length="cheese")
     12    badtype = models.CharField(max_length=type)
    1113    decimalfield = models.DecimalField()
    1214    filefield = models.FileField()
    1315    choices = models.CharField(max_length=10, choices='bad')
     
    184186
    185187
    186188model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute.
     189invalid_models.fielderrors: "badmax": CharFields require a "max_length" attribute of type int.
     190invalid_models.fielderrors: "badtype": CharFields require a "max_length" attribute of type int.
    187191invalid_models.fielderrors: "decimalfield": DecimalFields require a "decimal_places" attribute.
    188192invalid_models.fielderrors: "decimalfield": DecimalFields require a "max_digits" attribute.
    189193invalid_models.fielderrors: "filefield": FileFields require an "upload_to" attribute.
Back to Top