Code

Ticket #12404: 12404_patch.diff

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

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

Line 
1Index: django/core/management/validation.py
2===================================================================
3--- django/core/management/validation.py        (revision 12085)
4+++ django/core/management/validation.py        (working copy)
5@@ -37,8 +37,13 @@
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             if isinstance(f, models.DecimalField):
19                 if f.decimal_places is None:
20                     e.add(opts, '"%s": DecimalFields require a "decimal_places" attribute.' % f.name)