summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-05-10 21:09:38 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-05-10 21:09:38 +0000
commitfe971ec66f5d42945b2583037303be31f53ea524 (patch)
treebe0980d53d8e22767de3c502fc0d2b2c169d90e6
parent8c2db4ab0f87643765b6bffac7c524f5e2e54d65 (diff)
Changed r10668 to not falsely error out when using generic inlines.
The bug was picked up by the tests already, but only if run against a backend that supports referential integrity. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10732 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/admin/validation.py6
-rw-r--r--django/forms/models.py11
2 files changed, 11 insertions, 6 deletions
diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py
index 4bef007e6c..50e41437b8 100644
--- a/django/contrib/admin/validation.py
+++ b/django/contrib/admin/validation.py
@@ -169,11 +169,11 @@ def validate_inline(cls, parent, parent_model):
# exclude
if hasattr(cls, 'exclude') and cls.exclude:
- fk_name = _get_foreign_key(parent_model, cls.model).name
- if fk_name in cls.exclude:
+ fk = _get_foreign_key(parent_model, cls.model, can_fail=True)
+ if fk and fk.name in cls.exclude:
raise ImproperlyConfigured("%s cannot exclude the field "
"'%s' - this is the foreign key to the parent model "
- "%s." % (cls.__name__, fk_name, parent_model.__name__))
+ "%s." % (cls.__name__, fk.name, parent_model.__name__))
def validate_base(cls, model):
opts = model._meta
diff --git a/django/forms/models.py b/django/forms/models.py
index ce75bdcc3b..d42b98483b 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -756,10 +756,13 @@ class BaseInlineFormSet(BaseModelFormSet):
unique_check = [field for field in unique_check if field != self.fk.name]
return super(BaseInlineFormSet, self).get_unique_error_message(unique_check)
-def _get_foreign_key(parent_model, model, fk_name=None):
+def _get_foreign_key(parent_model, model, fk_name=None, can_fail=False):
"""
- Finds and returns the ForeignKey from model to parent if there is one.
- If fk_name is provided, assume it is the name of the ForeignKey field.
+ Finds and returns the ForeignKey from model to parent if there is one
+ (returns None if can_fail is True and no such field exists). If fk_name is
+ provided, assume it is the name of the ForeignKey field. Unles can_fail is
+ True, an exception is raised if there is no ForeignKey from model to
+ parent_model.
"""
# avoid circular import
from django.db.models import ForeignKey
@@ -785,6 +788,8 @@ def _get_foreign_key(parent_model, model, fk_name=None):
if len(fks_to_parent) == 1:
fk = fks_to_parent[0]
elif len(fks_to_parent) == 0:
+ if can_fail:
+ return
raise Exception("%s has no ForeignKey to %s" % (model, parent_model))
else:
raise Exception("%s has more than 1 ForeignKey to %s" % (model, parent_model))