Negligible refactoring in django.core.meta.fields and django.contrib.admin.views...
[fdr-django.git] / django / bin / validate.py
blob9e59fe7823e7b3de5632c783617dc3fb5638879e
1 from django.core import meta
3 def validate_app(app_label):
4 mod = meta.get_app(app_label)
5 for klass in mod._MODELS:
6 try:
7 validate_class(klass)
8 except AssertionError, e:
9 print e
11 def validate_class(klass):
12 opts = klass._meta
13 # Fields.
14 for f in opts.fields:
15 if isinstance(f, meta.ManyToManyField):
16 assert isinstance(f.rel, meta.ManyToMany), \
17 "ManyToManyField %s should have 'rel' set to a ManyToMany instance." % f.name
18 # Inline related objects.
19 for related in opts.get_followed_related_objects():
20 assert len([f for f in related.opts.fields if f.core]) > 0, \
21 "At least one field in %s should have core=True, because it's being edited inline by %s." % \
22 (related.opts.object_name, opts.object_name)
23 # All related objects.
24 related_apps_seen = []
25 for related in opts.get_all_related_objects():
26 if related.opts in related_apps_seen:
27 assert related.field.rel.related_name is not None, \
28 "Relationship in field %s.%s needs to set 'related_name' because more than one" \
29 " %s object is referenced in %s." % \
30 (related.opts.object_name, related.field.name, opts.object_name, rel_opts.object_name)
31 related_apps_seen.append(related.opts)
32 # Etc.
33 if opts.admin is not None:
34 assert opts.admin.ordering or opts.ordering, \
35 "%s needs to set 'ordering' on either its 'admin' or its model," \
36 " because it has 'admin' set." % opts.object_name
38 if __name__ == "__main__":
39 import sys
40 try:
41 validate_app(sys.argv[1])
42 except IndexError:
43 sys.stderr.write("Usage: %s [appname]\n" % __file__)
44 sys.exit(1)