|
1 from django import forms |
|
2 from django.forms.models import modelform_factory, inlineformset_factory, modelformset_factory |
|
3 from django.test import TestCase |
|
4 |
|
5 from models import User, UserSite, Restaurant, Manager |
|
6 |
|
7 |
|
8 class InlineFormsetTests(TestCase): |
|
9 def test_formset_over_to_field(self): |
|
10 "A formset over a ForeignKey with a to_field can be saved. Regression for #10243" |
|
11 Form = modelform_factory(User) |
|
12 FormSet = inlineformset_factory(User, UserSite) |
|
13 |
|
14 # Instantiate the Form and FormSet to prove |
|
15 # you can create a form with no data |
|
16 form = Form() |
|
17 form_set = FormSet(instance=User()) |
|
18 |
|
19 # Now create a new User and UserSite instance |
|
20 data = { |
|
21 'serial': u'1', |
|
22 'username': u'apollo13', |
|
23 'usersite_set-TOTAL_FORMS': u'1', |
|
24 'usersite_set-INITIAL_FORMS': u'0', |
|
25 'usersite_set-MAX_NUM_FORMS': u'0', |
|
26 'usersite_set-0-data': u'10', |
|
27 'usersite_set-0-user': u'apollo13' |
|
28 } |
|
29 user = User() |
|
30 form = Form(data) |
|
31 if form.is_valid(): |
|
32 user = form.save() |
|
33 else: |
|
34 self.fail('Errors found on form:%s' % form_set) |
|
35 |
|
36 form_set = FormSet(data, instance=user) |
|
37 if form_set.is_valid(): |
|
38 form_set.save() |
|
39 usersite = UserSite.objects.all().values() |
|
40 self.assertEqual(usersite[0]['data'], 10) |
|
41 self.assertEqual(usersite[0]['user_id'], u'apollo13') |
|
42 else: |
|
43 self.fail('Errors found on formset:%s' % form_set.errors) |
|
44 |
|
45 # Now update the UserSite instance |
|
46 data = { |
|
47 'usersite_set-TOTAL_FORMS': u'1', |
|
48 'usersite_set-INITIAL_FORMS': u'1', |
|
49 'usersite_set-MAX_NUM_FORMS': u'0', |
|
50 'usersite_set-0-id': unicode(usersite[0]['id']), |
|
51 'usersite_set-0-data': u'11', |
|
52 'usersite_set-0-user': u'apollo13' |
|
53 } |
|
54 form_set = FormSet(data, instance=user) |
|
55 if form_set.is_valid(): |
|
56 form_set.save() |
|
57 usersite = UserSite.objects.all().values() |
|
58 self.assertEqual(usersite[0]['data'], 11) |
|
59 self.assertEqual(usersite[0]['user_id'], u'apollo13') |
|
60 else: |
|
61 self.fail('Errors found on formset:%s' % form_set.errors) |
|
62 |
|
63 # Now add a new UserSite instance |
|
64 data = { |
|
65 'usersite_set-TOTAL_FORMS': u'2', |
|
66 'usersite_set-INITIAL_FORMS': u'1', |
|
67 'usersite_set-MAX_NUM_FORMS': u'0', |
|
68 'usersite_set-0-id': unicode(usersite[0]['id']), |
|
69 'usersite_set-0-data': u'11', |
|
70 'usersite_set-0-user': u'apollo13', |
|
71 'usersite_set-1-data': u'42', |
|
72 'usersite_set-1-user': u'apollo13' |
|
73 } |
|
74 form_set = FormSet(data, instance=user) |
|
75 if form_set.is_valid(): |
|
76 form_set.save() |
|
77 usersite = UserSite.objects.all().values().order_by('data') |
|
78 self.assertEqual(usersite[0]['data'], 11) |
|
79 self.assertEqual(usersite[0]['user_id'], u'apollo13') |
|
80 self.assertEqual(usersite[1]['data'], 42) |
|
81 self.assertEqual(usersite[1]['user_id'], u'apollo13') |
|
82 else: |
|
83 self.fail('Errors found on formset:%s' % form_set.errors) |
|
84 |
|
85 def test_formset_over_inherited_model(self): |
|
86 "A formset over a ForeignKey with a to_field can be saved. Regression for #11120" |
|
87 Form = modelform_factory(Restaurant) |
|
88 FormSet = inlineformset_factory(Restaurant, Manager) |
|
89 |
|
90 # Instantiate the Form and FormSet to prove |
|
91 # you can create a form with no data |
|
92 form = Form() |
|
93 form_set = FormSet(instance=Restaurant()) |
|
94 |
|
95 # Now create a new Restaurant and Manager instance |
|
96 data = { |
|
97 'name': u"Guido's House of Pasta", |
|
98 'manager_set-TOTAL_FORMS': u'1', |
|
99 'manager_set-INITIAL_FORMS': u'0', |
|
100 'manager_set-MAX_NUM_FORMS': u'0', |
|
101 'manager_set-0-name': u'Guido Van Rossum' |
|
102 } |
|
103 restaurant = User() |
|
104 form = Form(data) |
|
105 if form.is_valid(): |
|
106 restaurant = form.save() |
|
107 else: |
|
108 self.fail('Errors found on form:%s' % form_set) |
|
109 |
|
110 form_set = FormSet(data, instance=restaurant) |
|
111 if form_set.is_valid(): |
|
112 form_set.save() |
|
113 manager = Manager.objects.all().values() |
|
114 self.assertEqual(manager[0]['name'], 'Guido Van Rossum') |
|
115 else: |
|
116 self.fail('Errors found on formset:%s' % form_set.errors) |
|
117 |
|
118 # Now update the Manager instance |
|
119 data = { |
|
120 'manager_set-TOTAL_FORMS': u'1', |
|
121 'manager_set-INITIAL_FORMS': u'1', |
|
122 'manager_set-MAX_NUM_FORMS': u'0', |
|
123 'manager_set-0-id': unicode(manager[0]['id']), |
|
124 'manager_set-0-name': u'Terry Gilliam' |
|
125 } |
|
126 form_set = FormSet(data, instance=restaurant) |
|
127 if form_set.is_valid(): |
|
128 form_set.save() |
|
129 manager = Manager.objects.all().values() |
|
130 self.assertEqual(manager[0]['name'], 'Terry Gilliam') |
|
131 else: |
|
132 self.fail('Errors found on formset:%s' % form_set.errors) |
|
133 |
|
134 # Now add a new Manager instance |
|
135 data = { |
|
136 'manager_set-TOTAL_FORMS': u'2', |
|
137 'manager_set-INITIAL_FORMS': u'1', |
|
138 'manager_set-MAX_NUM_FORMS': u'0', |
|
139 'manager_set-0-id': unicode(manager[0]['id']), |
|
140 'manager_set-0-name': u'Terry Gilliam', |
|
141 'manager_set-1-name': u'John Cleese' |
|
142 } |
|
143 form_set = FormSet(data, instance=restaurant) |
|
144 if form_set.is_valid(): |
|
145 form_set.save() |
|
146 manager = Manager.objects.all().values().order_by('name') |
|
147 self.assertEqual(manager[0]['name'], 'John Cleese') |
|
148 self.assertEqual(manager[1]['name'], 'Terry Gilliam') |
|
149 else: |
|
150 self.fail('Errors found on formset:%s' % form_set.errors) |
|
151 |
|
152 def test_formset_with_none_instance(self): |
|
153 "A formset with instance=None can be created. Regression for #11872" |
|
154 Form = modelform_factory(User) |
|
155 FormSet = inlineformset_factory(User, UserSite) |
|
156 |
|
157 # Instantiate the Form and FormSet to prove |
|
158 # you can create a formset with an instance of None |
|
159 form = Form(instance=None) |
|
160 formset = FormSet(instance=None) |
|
161 |
|
162 |
|
163 class CustomWidget(forms.CharField): |
|
164 pass |
|
165 |
|
166 |
|
167 class UserSiteForm(forms.ModelForm): |
|
168 class Meta: |
|
169 model = UserSite |
|
170 widgets = {'data': CustomWidget} |
|
171 |
|
172 |
|
173 class Callback(object): |
|
174 |
|
175 def __init__(self): |
|
176 self.log = [] |
|
177 |
|
178 def __call__(self, db_field, **kwargs): |
|
179 self.log.append((db_field, kwargs)) |
|
180 return db_field.formfield(**kwargs) |
|
181 |
|
182 |
|
183 class FormfieldCallbackTests(TestCase): |
|
184 """ |
|
185 Regression for #13095: Using base forms with widgets |
|
186 defined in Meta should not raise errors. |
|
187 """ |
|
188 |
|
189 def test_inlineformset_factory_default(self): |
|
190 Formset = inlineformset_factory(User, UserSite, form=UserSiteForm) |
|
191 form = Formset({}).forms[0] |
|
192 self.assertTrue(isinstance(form['data'].field.widget, CustomWidget)) |
|
193 |
|
194 def test_modelformset_factory_default(self): |
|
195 Formset = modelformset_factory(UserSite, form=UserSiteForm) |
|
196 form = Formset({}).forms[0] |
|
197 self.assertTrue(isinstance(form['data'].field.widget, CustomWidget)) |
|
198 |
|
199 def assertCallbackCalled(self, callback): |
|
200 id_field, user_field, data_field = UserSite._meta.fields |
|
201 expected_log = [ |
|
202 (id_field, {}), |
|
203 (user_field, {}), |
|
204 (data_field, {'widget': CustomWidget}), |
|
205 ] |
|
206 self.assertEqual(callback.log, expected_log) |
|
207 |
|
208 def test_inlineformset_custom_callback(self): |
|
209 callback = Callback() |
|
210 inlineformset_factory(User, UserSite, form=UserSiteForm, |
|
211 formfield_callback=callback) |
|
212 self.assertCallbackCalled(callback) |
|
213 |
|
214 def test_modelformset_custom_callback(self): |
|
215 callback = Callback() |
|
216 modelformset_factory(UserSite, form=UserSiteForm, |
|
217 formfield_callback=callback) |
|
218 self.assertCallbackCalled(callback) |