| 111 | def test_deletion_without_commit(self): |
| 112 | """ |
| 113 | Make sure inlineformsets respect commit=False when deleting (#17988). |
| 114 | """ |
| 115 | PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True) |
| 116 | poet = Poet.objects.create(name='test') |
| 117 | poem = poet.poem_set.create(name='test poem') |
| 118 | data = { |
| 119 | 'poem_set-TOTAL_FORMS': u'1', |
| 120 | 'poem_set-INITIAL_FORMS': u'1', |
| 121 | 'poem_set-MAX_NUM_FORMS': u'0', |
| 122 | 'poem_set-0-id': unicode(poem.pk), |
| 123 | 'poem_set-0-poet': unicode(poet.pk), |
| 124 | 'poem_set-0-name': u'test', |
| 125 | } |
| 126 | formset = PoemFormSet(data, instance=poet) |
| 127 | self.assertTrue(formset.is_valid()) |
| 128 | formset.save() |
| 129 | self.assertEqual(Poem.objects.count(), 1) |
| 131 | data['poem_set-0-DELETE'] = 'on' |
| 132 | formset = PoemFormSet(data, instance=poet) |
| 133 | self.assertTrue(formset.is_valid()) |
| 134 | |
| 135 | poems = formset.save(commit=False) |
| 136 | self.assertEqual(poems, []) |
| 137 | self.assertEqual(len(formset.deleted_objects), 1) |
| 138 | self.assertEqual(Poem.objects.count(), 1) |
| 139 | |
| 140 | poems = formset.save() |
| 141 | self.assertEqual(poems, []) |
| 142 | self.assertEqual(len(formset.deleted_objects), 1) |
| 143 | self.assertEqual(Poem.objects.count(), 0) |
| 144 | |
| 145 | |