Skip to content

Commit

Permalink
Fixed #1 -- form instance is incorrectly propagated to other forms
Browse files Browse the repository at this point in the history
  • Loading branch information
t0ster committed Dec 20, 2011
1 parent 7356f00 commit bafc59c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
7 changes: 5 additions & 2 deletions composite_form/forms.py
Expand Up @@ -10,8 +10,8 @@ class CompositeForm(forms.Form):
class ProfileForm(CompositeForm):
form_list = [ProfileAddressForm, ProfileBirthDayForm]
"""
form_list = None
_form_instances = {}
form_list = None # Form classes
_form_instances = {} # Form instances

def __init__(self, data=None, files=None, *args, **initkwargs):
if "instance" in initkwargs:
Expand All @@ -22,6 +22,9 @@ def __init__(self, data=None, files=None, *args, **initkwargs):
raise ValueError("instances should be a list the same lenth as form_list")
if len(instances) != len(self.form_list):
raise ValueError("instances should be a list the same lenth as form_list")
for instance, form in zip(instances, self.form_list):
if instance is not None and not isinstance(instance, form._meta.model):
raise ValueError
self.is_bound = data is not None or files is not None
self.instances = instances

Expand Down
36 changes: 36 additions & 0 deletions example/tests/test_1.py
@@ -0,0 +1,36 @@
# Regression test for https://github.com/t0ster/django-composite-form/issues/1

from django.test import TestCase
from django.contrib.auth.models import User


from example.forms import ProfileForm, BaseProfileForm, UserCreationForm
from example.models import Profile


class FormsTests(TestCase):
def setUp(self):
profile_form = ProfileForm({
"address": "13 Test St",
"username": "test_user",
"password1": "123456",
"password2": "123456",
})
profile_form.save()
self.user = User.objects.all()[0]
self.profile = Profile.objects.all()[0]

def test_1(self):
profile_form = ProfileForm(instances=[self.user, self.profile])
self.assertEqual(profile_form.get_form(UserCreationForm).instance, self.user)
self.assertEqual(profile_form.get_form(BaseProfileForm).instance, self.profile)

def test_2(self):
profile_form = ProfileForm(instances=[self.user, None])
self.assertEqual(profile_form.get_form(UserCreationForm).instance, self.user)
self.assertIsInstance(profile_form.get_form(BaseProfileForm).instance, Profile)

def test_3(self):
profile_form = ProfileForm(instances=[None, self.profile])
self.assertEqual(profile_form.get_form(BaseProfileForm).instance, self.profile)
self.assertIsInstance(profile_form.get_form(UserCreationForm).instance, User)

0 comments on commit bafc59c

Please sign in to comment.