app/django/core/management/validation.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
--- a/app/django/core/management/validation.py	Tue Oct 14 12:36:55 2008 +0000
+++ b/app/django/core/management/validation.py	Tue Oct 14 16:00:59 2008 +0000
@@ -51,8 +51,6 @@
                     from PIL import Image
                 except ImportError:
                     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)
-            if f.prepopulate_from is not None and type(f.prepopulate_from) not in (list, tuple):
-                e.add(opts, '"%s": prepopulate_from should be a list or tuple.' % f.name)
             if f.choices:
                 if isinstance(f.choices, basestring) or not is_iterable(f.choices):
                     e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name)
@@ -63,17 +61,14 @@
             if f.db_index not in (None, True, False):
                 e.add(opts, '"%s": "db_index" should be either None, True or False.' % f.name)
 
-            # Check that max_length <= 255 if using older MySQL versions.
-            if settings.DATABASE_ENGINE == 'mysql':
-                db_version = connection.get_server_version()
-                if db_version < (5, 0, 3) and isinstance(f, (models.CharField, models.CommaSeparatedIntegerField, models.SlugField)) and f.max_length > 255:
-                    e.add(opts, '"%s": %s cannot have a "max_length" greater than 255 when you are using a version of MySQL prior to 5.0.3 (you are using %s).' % (f.name, f.__class__.__name__, '.'.join([str(n) for n in db_version[:3]])))
+            # Perform any backend-specific field validation.
+            connection.validation.validate_field(e, opts, f)
 
             # Check to see if the related field will clash with any existing
             # fields, m2m fields, m2m related objects or related objects
             if f.rel:
                 if f.rel.to not in models.get_models():
-                    e.add(opts, "'%s' has relation with model %s, which has not been installed" % (f.name, f.rel.to))
+                    e.add(opts, "'%s' has a relation with model %s, which has either not been installed or is abstract." % (f.name, f.rel.to))
                 # it is a string and we could not find the model it refers to
                 # so skip the next section
                 if isinstance(f.rel.to, (str, unicode)):
@@ -104,17 +99,65 @@
                         if r.get_accessor_name() == rel_query_name:
                             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))
 
+        seen_intermediary_signatures = []
         for i, f in enumerate(opts.local_many_to_many):
             # Check to see if the related m2m field will clash with any
             # existing fields, m2m fields, m2m related objects or related
             # objects
             if f.rel.to not in models.get_models():
-                e.add(opts, "'%s' has m2m relation with model %s, which has not been installed" % (f.name, f.rel.to))
+                e.add(opts, "'%s' has an m2m relation with model %s, which has either not been installed or is abstract." % (f.name, f.rel.to))
                 # it is a string and we could not find the model it refers to
                 # so skip the next section
                 if isinstance(f.rel.to, (str, unicode)):
                     continue
 
+            # Check that the field is not set to unique.  ManyToManyFields do not support unique.
+            if f.unique:
+                e.add(opts, "ManyToManyFields cannot be unique.  Remove the unique argument on '%s'." % f.name)
+
+            if getattr(f.rel, 'through', None) is not None:
+                if hasattr(f.rel, 'through_model'):
+                    from_model, to_model = cls, f.rel.to
+                    if from_model == to_model and f.rel.symmetrical:
+                        e.add(opts, "Many-to-many fields with intermediate tables cannot be symmetrical.")
+                    seen_from, seen_to, seen_self = False, False, 0
+                    for inter_field in f.rel.through_model._meta.fields:
+                        rel_to = getattr(inter_field.rel, 'to', None)
+                        if from_model == to_model: # relation to self
+                            if rel_to == from_model:
+                                seen_self += 1
+                            if seen_self > 2:
+                                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))
+                        else:
+                            if rel_to == from_model:
+                                if seen_from:
+                                    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))
+                                else:
+                                    seen_from = True
+                            elif rel_to == to_model:
+                                if seen_to:
+                                    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))
+                                else:
+                                    seen_to = True
+                    if f.rel.through_model not in models.get_models():
+                        e.add(opts, "'%s' specifies an m2m relation through model %s, which has not been installed." % (f.name, f.rel.through))
+                    signature = (f.rel.to, cls, f.rel.through_model)
+                    if signature in seen_intermediary_signatures:
+                        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))
+                    else:
+                        seen_intermediary_signatures.append(signature)
+                    seen_related_fk, seen_this_fk = False, False
+                    for field in f.rel.through_model._meta.fields:
+                        if field.rel:
+                            if not seen_related_fk and field.rel.to == f.rel.to:
+                                seen_related_fk = True
+                            elif field.rel.to == cls:
+                                seen_this_fk = True
+                    if not seen_related_fk or not seen_this_fk:
+                        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))
+                else:
+                    e.add(opts, "'%s' specifies an m2m relation through model %s, which has not been installed" % (f.name, f.rel.through))
+
             rel_opts = f.rel.to._meta
             rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name()
             rel_query_name = f.related_query_name()
@@ -145,54 +188,6 @@
                     if r.get_accessor_name() == rel_query_name:
                         e.add(opts, "Reverse query name for m2m 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))
 
-        # Check admin attribute.
-        if opts.admin is not None:
-            if not isinstance(opts.admin, models.AdminOptions):
-                e.add(opts, '"admin" attribute, if given, must be set to a models.AdminOptions() instance.')
-            else:
-                # list_display
-                if not isinstance(opts.admin.list_display, (list, tuple)):
-                    e.add(opts, '"admin.list_display", if given, must be set to a list or tuple.')
-                else:
-                    for fn in opts.admin.list_display:
-                        try:
-                            f = opts.get_field(fn)
-                        except models.FieldDoesNotExist:
-                            if not hasattr(cls, fn):
-                                e.add(opts, '"admin.list_display" refers to %r, which isn\'t an attribute, method or property.' % fn)
-                        else:
-                            if isinstance(f, models.ManyToManyField):
-                                e.add(opts, '"admin.list_display" doesn\'t support ManyToManyFields (%r).' % fn)
-                # list_display_links
-                if opts.admin.list_display_links and not opts.admin.list_display:
-                    e.add(opts, '"admin.list_display" must be defined for "admin.list_display_links" to be used.')
-                if not isinstance(opts.admin.list_display_links, (list, tuple)):
-                    e.add(opts, '"admin.list_display_links", if given, must be set to a list or tuple.')
-                else:
-                    for fn in opts.admin.list_display_links:
-                        try:
-                            f = opts.get_field(fn)
-                        except models.FieldDoesNotExist:
-                            if not hasattr(cls, fn):
-                                e.add(opts, '"admin.list_display_links" refers to %r, which isn\'t an attribute, method or property.' % fn)
-                        if fn not in opts.admin.list_display:
-                            e.add(opts, '"admin.list_display_links" refers to %r, which is not defined in "admin.list_display".' % fn)
-                # list_filter
-                if not isinstance(opts.admin.list_filter, (list, tuple)):
-                    e.add(opts, '"admin.list_filter", if given, must be set to a list or tuple.')
-                else:
-                    for fn in opts.admin.list_filter:
-                        try:
-                            f = opts.get_field(fn)
-                        except models.FieldDoesNotExist:
-                            e.add(opts, '"admin.list_filter" refers to %r, which isn\'t a field.' % fn)
-                # date_hierarchy
-                if opts.admin.date_hierarchy:
-                    try:
-                        f = opts.get_field(opts.admin.date_hierarchy)
-                    except models.FieldDoesNotExist:
-                        e.add(opts, '"admin.date_hierarchy" refers to %r, which isn\'t a field.' % opts.admin.date_hierarchy)
-
         # Check ordering attribute.
         if opts.ordering:
             for field_name in opts.ordering:
@@ -210,18 +205,6 @@
                 except models.FieldDoesNotExist:
                     e.add(opts, '"ordering" refers to "%s", a field that doesn\'t exist.' % field_name)
 
-        # Check core=True, if needed.
-        for related in opts.get_followed_related_objects():
-            if not related.edit_inline:
-                continue
-            try:
-                for f in related.opts.fields:
-                    if f.core:
-                        raise StopIteration
-                e.add(related.opts, "At least one field in %s should have core=True, because it's being edited inline by %s.%s." % (related.opts.object_name, opts.module_name, opts.object_name))
-            except StopIteration:
-                pass
-
         # Check unique_together.
         for ut in opts.unique_together:
             for field_name in ut: