Skip to content

Commit

Permalink
[1.0.X] Changed r10672 to not falsely error out when using generic in…
Browse files Browse the repository at this point in the history
…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
malcolmt committed May 11, 2009
1 parent 1b6fac1 commit 9b86444
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions django/contrib/admin/validation.py
Expand Up @@ -140,11 +140,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
Expand Down
11 changes: 8 additions & 3 deletions django/forms/models.py
Expand Up @@ -727,10 +727,13 @@ def get_unique_error_message(self, unique_check):
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
Expand All @@ -756,6 +759,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))
Expand Down

0 comments on commit 9b86444

Please sign in to comment.