| 464 | def test_delete_fields_are_not_added_for_extra_formset_forms(self): |
| 465 | |
| 466 | """ |
| 467 | When a formset is built with can_delete=True and extra>=1, the extra |
| 468 | forms should not be seen to hold DELETE fields. |
| 469 | """ |
| 470 | |
| 471 | class GenericForm(Form): |
| 472 | foo = CharField() |
| 473 | |
| 474 | GenericFormFormset = formset_factory( |
| 475 | form=GenericForm, |
| 476 | can_delete=True, |
| 477 | can_delete_extra=False, |
| 478 | extra=2) |
| 479 | |
| 480 | # Instantiate formset and check DELETE fields do not exist |
| 481 | |
| 482 | formset = GenericFormFormset() |
| 483 | |
| 484 | self.assertEqual(len(formset), 2) |
| 485 | |
| 486 | self.assertNotIn('DELETE', formset.forms[0].fields) |
| 487 | self.assertNotIn('DELETE', formset.forms[1].fields) |
| 488 | |
| 489 | # Instantiate formset with initial data, check DELETE's do not exist |
| 490 | |
| 491 | formset = GenericFormFormset(initial=[{'foo': 'bar'}]) |
| 492 | |
| 493 | self.assertEqual(len(formset), 3) |
| 494 | |
| 495 | self.assertIn('DELETE', formset.forms[0].fields) |
| 496 | self.assertNotIn('DELETE', formset.forms[1].fields) |
| 497 | self.assertNotIn('DELETE', formset.forms[2].fields) |
| 498 | |
| 499 | # Ensure validation behaviour of formsets works correctly |
| 500 | |
| 501 | formset = GenericFormFormset(data={ |
| 502 | 'form-0-foo': 'bar', |
| 503 | 'form-0-DELETE': 'on', |
| 504 | 'form-1-foo': 'baz', |
| 505 | 'form-2-foo': '', |
| 506 | 'form-TOTAL_FORMS': '3', |
| 507 | 'form-INITIAL_FORMS': '1', |
| 508 | }, initial=[{'foo': 'bar'}]) |
| 509 | |
| 510 | # Check correct cleaned data from the formset |
| 511 | |
| 512 | self.assertEqual(formset.cleaned_data, [ |
| 513 | {'foo': u'bar', u'DELETE': True}, |
| 514 | {'foo': u'baz'}, |
| 515 | {}, |
| 516 | ]) |
| 517 | |
| 518 | # Ensure deletion flag is correctly detected by formset method |
| 519 | |
| 520 | self.assertTrue(formset._should_delete_form(formset.forms[0])) |
| 521 | self.assertFalse(formset._should_delete_form(formset.forms[1])) |
| 522 | self.assertFalse(formset._should_delete_form(formset.forms[2])) |
| 523 | |