2
from django.db import models
4
class School(models.Model):
5
name = models.CharField(max_length=100)
7
class Parent(models.Model):
8
name = models.CharField(max_length=100)
10
class Child(models.Model):
11
mother = models.ForeignKey(Parent, related_name='mothers_children')
12
father = models.ForeignKey(Parent, related_name='fathers_children')
13
school = models.ForeignKey(School)
14
name = models.CharField(max_length=100)
16
__test__ = {'API_TESTS': """
18
>>> from django.forms.models import inlineformset_factory
21
Child has two ForeignKeys to Parent, so if we don't specify which one to use
22
for the inline formset, we should get an exception.
24
>>> ifs = inlineformset_factory(Parent, Child)
25
Traceback (most recent call last):
27
Exception: <class 'regressiontests.inline_formsets.models.Child'> has more than 1 ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>
30
These two should both work without a problem.
32
>>> ifs = inlineformset_factory(Parent, Child, fk_name='mother')
33
>>> ifs = inlineformset_factory(Parent, Child, fk_name='father')
36
If we specify fk_name, but it isn't a ForeignKey from the child model to the
37
parent model, we should get an exception.
39
>>> ifs = inlineformset_factory(Parent, Child, fk_name='school')
40
Traceback (most recent call last):
42
Exception: fk_name 'school' is not a ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>
45
If the field specified in fk_name is not a ForeignKey, we should get an
48
>>> ifs = inlineformset_factory(Parent, Child, fk_name='test')
49
Traceback (most recent call last):
51
Exception: <class 'regressiontests.inline_formsets.models.Child'> has no field named 'test'