mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
newforms-admin: Fixed #5488. inlines with multiple ForeignKeys to the same parent don't work. Thanks jdetaeye. Also, some whitespace cleanup. Sorry for the mess.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6301 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
b28be9b04d
commit
f68769e1c6
@ -323,6 +323,14 @@ def inline_formset(parent_model, model, fk_name=None, fields=None, extra=3, orde
|
||||
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))
|
||||
else:
|
||||
fks_to_parent = [f for f in opts.fields if f.name == fk_name]
|
||||
if len(fks_to_parent) == 1:
|
||||
fk = fks_to_parent[0]
|
||||
if not isinstance(fk, ForeignKey) or fk.rel.to != parent_model:
|
||||
raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model))
|
||||
elif len(fks_to_parent) == 0:
|
||||
raise Exception("%s has no field named '%s'" % (model, fk_name))
|
||||
# let the formset handle object deletion by default
|
||||
FormSet = formset_for_model(model, formset=InlineFormset, fields=fields,
|
||||
formfield_callback=formfield_callback,
|
||||
|
0
tests/regressiontests/inline_formsets/__init__.py
Normal file
0
tests/regressiontests/inline_formsets/__init__.py
Normal file
55
tests/regressiontests/inline_formsets/models.py
Normal file
55
tests/regressiontests/inline_formsets/models.py
Normal file
@ -0,0 +1,55 @@
|
||||
# coding: utf-8
|
||||
from django.db import models
|
||||
|
||||
class School(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
class Parent(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
class Child(models.Model):
|
||||
mother = models.ForeignKey(Parent, related_name='mothers_children')
|
||||
father = models.ForeignKey(Parent, related_name='fathers_children')
|
||||
school = models.ForeignKey(School)
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
__test__ = {'API_TESTS': """
|
||||
|
||||
>>> from django.newforms.models import inline_formset
|
||||
|
||||
|
||||
Child has two ForeignKeys to Parent, so if we don't specify which one to use
|
||||
for the inline formset, we should get an exception.
|
||||
|
||||
>>> ifs = inline_formset(Parent, Child)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
Exception: <class 'regressiontests.inline_formsets.models.Child'> has more than 1 ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>
|
||||
|
||||
|
||||
These two should both work without a problem.
|
||||
|
||||
>>> ifs = inline_formset(Parent, Child, fk_name='mother')
|
||||
>>> ifs = inline_formset(Parent, Child, fk_name='father')
|
||||
|
||||
|
||||
If we specify fk_name, but it isn't a ForeignKey from the child model to the
|
||||
parent model, we should get an exception.
|
||||
|
||||
>>> ifs = inline_formset(Parent, Child, fk_name='school')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
Exception: fk_name 'school' is not a ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>
|
||||
|
||||
|
||||
If the field specified in fk_name is not a ForeignKey, we should get an
|
||||
exception.
|
||||
|
||||
>>> ifs = inline_formset(Parent, Child, fk_name='test')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
Exception: <class 'regressiontests.inline_formsets.models.Child'> has no field named 'test'
|
||||
|
||||
|
||||
"""
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user