Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Changed r10672 to not falsely error out when using generic in…

…lines.

The bug was picked up by the tests already, but only if run against a
backend that supports referential integrity.

Backport of r10732 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10736 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9b8644407d12b5aab52853578812d4b929201c16 1 parent 1b6fac1
Malcolm Tredinnick authored May 11, 2009
6  django/contrib/admin/validation.py
@@ -140,11 +140,11 @@ def validate_inline(cls, parent, parent_model):
140 140
 
141 141
     # exclude
142 142
     if hasattr(cls, 'exclude') and cls.exclude:
143  
-        fk_name = _get_foreign_key(parent_model, cls.model).name
144  
-        if fk_name in cls.exclude:
  143
+        fk = _get_foreign_key(parent_model, cls.model, can_fail=True)
  144
+        if fk and fk.name in cls.exclude:
145 145
             raise ImproperlyConfigured("%s cannot exclude the field "
146 146
                     "'%s' - this is the foreign key to the parent model "
147  
-                    "%s." % (cls.__name__, fk_name, parent_model.__name__))
  147
+                    "%s." % (cls.__name__, fk.name, parent_model.__name__))
148 148
 
149 149
 def validate_base(cls, model):
150 150
     opts = model._meta
11  django/forms/models.py
@@ -727,10 +727,13 @@ def get_unique_error_message(self, unique_check):
727 727
         unique_check = [field for field in unique_check if field != self.fk.name]
728 728
         return super(BaseInlineFormSet, self).get_unique_error_message(unique_check)
729 729
 
730  
-def _get_foreign_key(parent_model, model, fk_name=None):
  730
+def _get_foreign_key(parent_model, model, fk_name=None, can_fail=False):
731 731
     """
732  
-    Finds and returns the ForeignKey from model to parent if there is one.
733  
-    If fk_name is provided, assume it is the name of the ForeignKey field.
  732
+    Finds and returns the ForeignKey from model to parent if there is one
  733
+    (returns None if can_fail is True and no such field exists). If fk_name is
  734
+    provided, assume it is the name of the ForeignKey field. Unles can_fail is
  735
+    True, an exception is raised if there is no ForeignKey from model to
  736
+    parent_model.
734 737
     """
735 738
     # avoid circular import
736 739
     from django.db.models import ForeignKey
@@ -756,6 +759,8 @@ def _get_foreign_key(parent_model, model, fk_name=None):
756 759
         if len(fks_to_parent) == 1:
757 760
             fk = fks_to_parent[0]
758 761
         elif len(fks_to_parent) == 0:
  762
+            if can_fail:
  763
+                return
759 764
             raise Exception("%s has no ForeignKey to %s" % (model, parent_model))
760 765
         else:
761 766
             raise Exception("%s has more than 1 ForeignKey to %s" % (model, parent_model))

0 notes on commit 9b86444

Please sign in to comment.
Something went wrong with that request. Please try again.