Code

Ticket #12404: 12404_new.diff

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

updated patch with tests

Line 
1Index: django/core/management/validation.py
2===================================================================
3--- django/core/management/validation.py        (revision 12677)
4+++ django/core/management/validation.py        (working copy)
5@@ -37,8 +37,19 @@
6                 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)
7             if f.name.endswith('_'):
8                 e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name)
9-            if isinstance(f, models.CharField) and f.max_length in (None, 0):
10-                e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name)
11+            if isinstance(f, models.CharField):
12+                try:
13+                    max_length = int(f.max_length)
14+                    if max_length <= 0:
15+                        e.add(opts, '"%s": CharFields require a "max_length attribute greater than zero."' % f.name)
16+                except ValueError:
17+                    e.add(opts, '"%s": CharFields require a "max_length" attribute of type int.' % f.name)
18+                except TypeError:
19+                    if f.max_length is None:
20+                        e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name)
21+                    else:
22+                        e.add(opts, '"%s": CharFields require a "max_length" attribute of type int.' % f.name)
23+                   
24             if isinstance(f, models.DecimalField):
25                 if f.decimal_places is None:
26                     e.add(opts, '"%s": DecimalFields require a "decimal_places" attribute.' % f.name)
27Index: tests/modeltests/invalid_models/models.py
28===================================================================
29--- tests/modeltests/invalid_models/models.py   (revision 12677)
30+++ tests/modeltests/invalid_models/models.py   (working copy)
31@@ -8,6 +8,8 @@
32 
33 class FieldErrors(models.Model):
34     charfield = models.CharField()
35+    badmax = models.CharField(max_length="cheese")
36+    badtype = models.CharField(max_length=type)
37     decimalfield = models.DecimalField()
38     filefield = models.FileField()
39     choices = models.CharField(max_length=10, choices='bad')
40@@ -184,6 +186,8 @@
41 
42 
43 model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute.
44+invalid_models.fielderrors: "badmax": CharFields require a "max_length" attribute of type int.
45+invalid_models.fielderrors: "badtype": CharFields require a "max_length" attribute of type int.
46 invalid_models.fielderrors: "decimalfield": DecimalFields require a "decimal_places" attribute.
47 invalid_models.fielderrors: "decimalfield": DecimalFields require a "max_digits" attribute.
48 invalid_models.fielderrors: "filefield": FileFields require an "upload_to" attribute.