35 for f in opts.local_fields: |
35 for f in opts.local_fields: |
36 if f.name == 'id' and not f.primary_key and opts.pk.name == 'id': |
36 if f.name == 'id' and not f.primary_key and opts.pk.name == 'id': |
37 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) |
37 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) |
38 if f.name.endswith('_'): |
38 if f.name.endswith('_'): |
39 e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name) |
39 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): |
40 if isinstance(f, models.CharField): |
41 e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name) |
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 that is a positive integer.' % f.name) |
|
45 except (ValueError, TypeError): |
|
46 e.add(opts, '"%s": CharFields require a "max_length" attribute that is a positive integer.' % f.name) |
42 if isinstance(f, models.DecimalField): |
47 if isinstance(f, models.DecimalField): |
43 if f.decimal_places is None: |
48 decimalp_msg ='"%s": DecimalFields require a "decimal_places" attribute that is a non-negative integer.' |
44 e.add(opts, '"%s": DecimalFields require a "decimal_places" attribute.' % f.name) |
49 try: |
45 if f.max_digits is None: |
50 decimal_places = int(f.decimal_places) |
46 e.add(opts, '"%s": DecimalFields require a "max_digits" attribute.' % f.name) |
51 if decimal_places < 0: |
|
52 e.add(opts, decimalp_msg % f.name) |
|
53 except (ValueError, TypeError): |
|
54 e.add(opts, decimalp_msg % f.name) |
|
55 mdigits_msg = '"%s": DecimalFields require a "max_digits" attribute that is a positive integer.' |
|
56 try: |
|
57 max_digits = int(f.max_digits) |
|
58 if max_digits <= 0: |
|
59 e.add(opts, mdigits_msg % f.name) |
|
60 except (ValueError, TypeError): |
|
61 e.add(opts, mdigits_msg % f.name) |
47 if isinstance(f, models.FileField) and not f.upload_to: |
62 if isinstance(f, models.FileField) and not f.upload_to: |
48 e.add(opts, '"%s": FileFields require an "upload_to" attribute.' % f.name) |
63 e.add(opts, '"%s": FileFields require an "upload_to" attribute.' % f.name) |
49 if isinstance(f, models.ImageField): |
64 if isinstance(f, models.ImageField): |
|
65 # Try to import PIL in either of the two ways it can end up installed. |
50 try: |
66 try: |
51 from PIL import Image |
67 from PIL import Image |
52 except ImportError: |
68 except ImportError: |
53 e.add(opts, '"%s": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .' % f.name) |
69 try: |
|
70 import Image |
|
71 except ImportError: |
|
72 e.add(opts, '"%s": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .' % f.name) |
54 if isinstance(f, models.BooleanField) and getattr(f, 'null', False): |
73 if isinstance(f, models.BooleanField) and getattr(f, 'null', False): |
55 e.add(opts, '"%s": BooleanFields do not accept null values. Use a NullBooleanField instead.' % f.name) |
74 e.add(opts, '"%s": BooleanFields do not accept null values. Use a NullBooleanField instead.' % f.name) |
56 if f.choices: |
75 if f.choices: |
57 if isinstance(f.choices, basestring) or not is_iterable(f.choices): |
76 if isinstance(f.choices, basestring) or not is_iterable(f.choices): |
58 e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name) |
77 e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name) |
59 else: |
78 else: |
60 for c in f.choices: |
79 for c in f.choices: |
61 if not type(c) in (tuple, list) or len(c) != 2: |
80 if not isinstance(c, (list, tuple)) or len(c) != 2: |
62 e.add(opts, '"%s": "choices" should be a sequence of two-tuples.' % f.name) |
81 e.add(opts, '"%s": "choices" should be a sequence of two-tuples.' % f.name) |
63 if f.db_index not in (None, True, False): |
82 if f.db_index not in (None, True, False): |
64 e.add(opts, '"%s": "db_index" should be either None, True or False.' % f.name) |
83 e.add(opts, '"%s": "db_index" should be either None, True or False.' % f.name) |
65 |
84 |
66 # Perform any backend-specific field validation. |
85 # Perform any backend-specific field validation. |
74 # it is a string and we could not find the model it refers to |
93 # it is a string and we could not find the model it refers to |
75 # so skip the next section |
94 # so skip the next section |
76 if isinstance(f.rel.to, (str, unicode)): |
95 if isinstance(f.rel.to, (str, unicode)): |
77 continue |
96 continue |
78 |
97 |
|
98 # Make sure the related field specified by a ForeignKey is unique |
|
99 if not f.rel.to._meta.get_field(f.rel.field_name).unique: |
|
100 e.add(opts, "Field '%s' under model '%s' must have a unique=True constraint." % (f.rel.field_name, f.rel.to.__name__)) |
|
101 |
79 rel_opts = f.rel.to._meta |
102 rel_opts = f.rel.to._meta |
80 rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() |
103 rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() |
81 rel_query_name = f.related_query_name() |
104 rel_query_name = f.related_query_name() |
82 for r in rel_opts.fields: |
105 if not f.rel.is_hidden(): |
83 if r.name == rel_name: |
106 for r in rel_opts.fields: |
84 e.add(opts, "Accessor for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name)) |
107 if r.name == rel_name: |
85 if r.name == rel_query_name: |
108 e.add(opts, "Accessor for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name)) |
86 e.add(opts, "Reverse query name for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name)) |
109 if r.name == rel_query_name: |
87 for r in rel_opts.local_many_to_many: |
110 e.add(opts, "Reverse query name for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name)) |
88 if r.name == rel_name: |
111 for r in rel_opts.local_many_to_many: |
89 e.add(opts, "Accessor for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name)) |
112 if r.name == rel_name: |
90 if r.name == rel_query_name: |
113 e.add(opts, "Accessor for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name)) |
91 e.add(opts, "Reverse query name for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name)) |
114 if r.name == rel_query_name: |
92 for r in rel_opts.get_all_related_many_to_many_objects(): |
115 e.add(opts, "Reverse query name for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name)) |
93 if r.get_accessor_name() == rel_name: |
116 for r in rel_opts.get_all_related_many_to_many_objects(): |
94 e.add(opts, "Accessor for field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name)) |
|
95 if r.get_accessor_name() == rel_query_name: |
|
96 e.add(opts, "Reverse query name for field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name)) |
|
97 for r in rel_opts.get_all_related_objects(): |
|
98 if r.field is not f: |
|
99 if r.get_accessor_name() == rel_name: |
117 if r.get_accessor_name() == rel_name: |
100 e.add(opts, "Accessor for field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name)) |
118 e.add(opts, "Accessor for field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name)) |
101 if r.get_accessor_name() == rel_query_name: |
119 if r.get_accessor_name() == rel_query_name: |
102 e.add(opts, "Reverse query name for field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name)) |
120 e.add(opts, "Reverse query name for field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name)) |
|
121 for r in rel_opts.get_all_related_objects(): |
|
122 if r.field is not f: |
|
123 if r.get_accessor_name() == rel_name: |
|
124 e.add(opts, "Accessor for field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name)) |
|
125 if r.get_accessor_name() == rel_query_name: |
|
126 e.add(opts, "Reverse query name for field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name)) |
103 |
127 |
104 seen_intermediary_signatures = [] |
128 seen_intermediary_signatures = [] |
105 for i, f in enumerate(opts.local_many_to_many): |
129 for i, f in enumerate(opts.local_many_to_many): |
106 # Check to see if the related m2m field will clash with any |
130 # Check to see if the related m2m field will clash with any |
107 # existing fields, m2m fields, m2m related objects or related |
131 # existing fields, m2m fields, m2m related objects or related |
115 |
139 |
116 # Check that the field is not set to unique. ManyToManyFields do not support unique. |
140 # Check that the field is not set to unique. ManyToManyFields do not support unique. |
117 if f.unique: |
141 if f.unique: |
118 e.add(opts, "ManyToManyFields cannot be unique. Remove the unique argument on '%s'." % f.name) |
142 e.add(opts, "ManyToManyFields cannot be unique. Remove the unique argument on '%s'." % f.name) |
119 |
143 |
120 if getattr(f.rel, 'through', None) is not None: |
144 if f.rel.through is not None and not isinstance(f.rel.through, basestring): |
121 if hasattr(f.rel, 'through_model'): |
145 from_model, to_model = cls, f.rel.to |
122 from_model, to_model = cls, f.rel.to |
146 if from_model == to_model and f.rel.symmetrical and not f.rel.through._meta.auto_created: |
123 if from_model == to_model and f.rel.symmetrical: |
147 e.add(opts, "Many-to-many fields with intermediate tables cannot be symmetrical.") |
124 e.add(opts, "Many-to-many fields with intermediate tables cannot be symmetrical.") |
148 seen_from, seen_to, seen_self = False, False, 0 |
125 seen_from, seen_to, seen_self = False, False, 0 |
149 for inter_field in f.rel.through._meta.fields: |
126 for inter_field in f.rel.through_model._meta.fields: |
150 rel_to = getattr(inter_field.rel, 'to', None) |
127 rel_to = getattr(inter_field.rel, 'to', None) |
151 if from_model == to_model: # relation to self |
128 if from_model == to_model: # relation to self |
152 if rel_to == from_model: |
129 if rel_to == from_model: |
153 seen_self += 1 |
130 seen_self += 1 |
154 if seen_self > 2: |
131 if seen_self > 2: |
155 e.add(opts, "Intermediary model %s has more than " |
132 e.add(opts, "Intermediary model %s has more than two foreign keys to %s, which is ambiguous and is not permitted." % (f.rel.through_model._meta.object_name, from_model._meta.object_name)) |
156 "two foreign keys to %s, which is ambiguous " |
133 else: |
157 "and is not permitted." % ( |
134 if rel_to == from_model: |
158 f.rel.through._meta.object_name, |
135 if seen_from: |
159 from_model._meta.object_name |
136 e.add(opts, "Intermediary model %s has more than one foreign key to %s, which is ambiguous and is not permitted." % (f.rel.through_model._meta.object_name, from_model._meta.object_name)) |
160 ) |
137 else: |
161 ) |
138 seen_from = True |
|
139 elif rel_to == to_model: |
|
140 if seen_to: |
|
141 e.add(opts, "Intermediary model %s has more than one foreign key to %s, which is ambiguous and is not permitted." % (f.rel.through_model._meta.object_name, rel_to._meta.object_name)) |
|
142 else: |
|
143 seen_to = True |
|
144 if f.rel.through_model not in models.get_models(): |
|
145 e.add(opts, "'%s' specifies an m2m relation through model %s, which has not been installed." % (f.name, f.rel.through)) |
|
146 signature = (f.rel.to, cls, f.rel.through_model) |
|
147 if signature in seen_intermediary_signatures: |
|
148 e.add(opts, "The model %s has two manually-defined m2m relations through the model %s, which is not permitted. Please consider using an extra field on your intermediary model instead." % (cls._meta.object_name, f.rel.through_model._meta.object_name)) |
|
149 else: |
162 else: |
150 seen_intermediary_signatures.append(signature) |
163 if rel_to == from_model: |
|
164 if seen_from: |
|
165 e.add(opts, "Intermediary model %s has more " |
|
166 "than one foreign key to %s, which is " |
|
167 "ambiguous and is not permitted." % ( |
|
168 f.rel.through._meta.object_name, |
|
169 from_model._meta.object_name |
|
170 ) |
|
171 ) |
|
172 else: |
|
173 seen_from = True |
|
174 elif rel_to == to_model: |
|
175 if seen_to: |
|
176 e.add(opts, "Intermediary model %s has more " |
|
177 "than one foreign key to %s, which is " |
|
178 "ambiguous and is not permitted." % ( |
|
179 f.rel.through._meta.object_name, |
|
180 rel_to._meta.object_name |
|
181 ) |
|
182 ) |
|
183 else: |
|
184 seen_to = True |
|
185 if f.rel.through not in models.get_models(include_auto_created=True): |
|
186 e.add(opts, "'%s' specifies an m2m relation through model " |
|
187 "%s, which has not been installed." % (f.name, f.rel.through) |
|
188 ) |
|
189 signature = (f.rel.to, cls, f.rel.through) |
|
190 if signature in seen_intermediary_signatures: |
|
191 e.add(opts, "The model %s has two manually-defined m2m " |
|
192 "relations through the model %s, which is not " |
|
193 "permitted. Please consider using an extra field on " |
|
194 "your intermediary model instead." % ( |
|
195 cls._meta.object_name, |
|
196 f.rel.through._meta.object_name |
|
197 ) |
|
198 ) |
|
199 else: |
|
200 seen_intermediary_signatures.append(signature) |
|
201 if not f.rel.through._meta.auto_created: |
151 seen_related_fk, seen_this_fk = False, False |
202 seen_related_fk, seen_this_fk = False, False |
152 for field in f.rel.through_model._meta.fields: |
203 for field in f.rel.through._meta.fields: |
153 if field.rel: |
204 if field.rel: |
154 if not seen_related_fk and field.rel.to == f.rel.to: |
205 if not seen_related_fk and field.rel.to == f.rel.to: |
155 seen_related_fk = True |
206 seen_related_fk = True |
156 elif field.rel.to == cls: |
207 elif field.rel.to == cls: |
157 seen_this_fk = True |
208 seen_this_fk = True |
158 if not seen_related_fk or not seen_this_fk: |
209 if not seen_related_fk or not seen_this_fk: |
159 e.add(opts, "'%s' has a manually-defined m2m relation through model %s, which does not have foreign keys to %s and %s" % (f.name, f.rel.through, f.rel.to._meta.object_name, cls._meta.object_name)) |
210 e.add(opts, "'%s' is a manually-defined m2m relation " |
160 else: |
211 "through model %s, which does not have foreign keys " |
161 e.add(opts, "'%s' specifies an m2m relation through model %s, which has not been installed" % (f.name, f.rel.through)) |
212 "to %s and %s" % (f.name, f.rel.through._meta.object_name, |
|
213 f.rel.to._meta.object_name, cls._meta.object_name) |
|
214 ) |
|
215 elif isinstance(f.rel.through, basestring): |
|
216 e.add(opts, "'%s' specifies an m2m relation through model %s, " |
|
217 "which has not been installed" % (f.name, f.rel.through) |
|
218 ) |
162 |
219 |
163 rel_opts = f.rel.to._meta |
220 rel_opts = f.rel.to._meta |
164 rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() |
221 rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() |
165 rel_query_name = f.related_query_name() |
222 rel_query_name = f.related_query_name() |
166 # If rel_name is none, there is no reverse accessor (this only |
223 # If rel_name is none, there is no reverse accessor (this only |