16 | | BookWithOptionalAltEditor, AlternateBook, AuthorMeeting, CustomPrimaryKey, |
17 | | Place, Owner, Location, OwnerProfile, Restaurant, Product, Price, |
18 | | MexicanRestaurant, ClassyMexicanRestaurant, Repository, Revision, |
19 | | Person, Membership, Team, Player, Poet, Poem, Post) |
| 16 | BookWithOptionalAltEditor, BookWithOptionalISBN, AlternateBook, |
| 17 | AuthorMeeting, CustomPrimaryKey, Place, Owner, Location, OwnerProfile, |
| 18 | Restaurant, Product, Price, MexicanRestaurant, ClassyMexicanRestaurant, |
| 19 | Repository, Revision, Person, Membership, Team, Player, Poet, Poem, Post) |
| 642 | def test_inline_formsets_with_nullable_unique(self): |
| 643 | """ |
| 644 | Fields that have null=True and have None should be excluded from unique |
| 645 | checks. However, you cannot pass in None values via HTML rendered |
| 646 | forms. "Most" to_python methods will make it an empty string instead. |
| 647 | """ |
| 648 | |
| 649 | AuthorBooksFormSet = inlineformset_factory(Author, BookWithOptionalISBN, can_delete=False, extra=2) |
| 650 | author = Author.objects.create(pk=1, name='Charles Baudelaire') |
| 651 | |
| 652 | # Empty unique/nullable isbn (CharField) |
| 653 | data = { |
| 654 | 'bookwithoptionalisbn_set-TOTAL_FORMS': '2', # the number of forms rendered |
| 655 | 'bookwithoptionalisbn_set-INITIAL_FORMS': '0', # the number of forms with initial data |
| 656 | 'bookwithoptionalisbn_set-MAX_NUM_FORMS': '', # the max number of forms |
| 657 | 'bookwithoptionalisbn_set-0-author': '1', |
| 658 | 'bookwithoptionalisbn_set-0-priority': '1', |
| 659 | 'bookwithoptionalisbn_set-0-title': 'Het gouden ei', |
| 660 | 'bookwithoptionalisbn_set-1-author': '1', |
| 661 | 'bookwithoptionalisbn_set-1-priority': '2', |
| 662 | 'bookwithoptionalisbn_set-1-title': 'Fokke en Sukke', |
| 663 | } |
| 664 | formset = AuthorBooksFormSet(data, instance=author) |
| 665 | self.assertFalse(formset.is_valid(), formset.errors) |
| 666 | |
| 667 | # Empty unique/nullable priority (IntegerField) |
| 668 | data = { |
| 669 | 'bookwithoptionalisbn_set-TOTAL_FORMS': '2', # the number of forms rendered |
| 670 | 'bookwithoptionalisbn_set-INITIAL_FORMS': '0', # the number of forms with initial data |
| 671 | 'bookwithoptionalisbn_set-MAX_NUM_FORMS': '', # the max number of forms |
| 672 | 'bookwithoptionalisbn_set-0-author': '1', |
| 673 | 'bookwithoptionalisbn_set-0-isbn': '1234', |
| 674 | 'bookwithoptionalisbn_set-0-title': 'Het gouden ei', |
| 675 | 'bookwithoptionalisbn_set-1-author': '1', |
| 676 | 'bookwithoptionalisbn_set-1-isbn': '5678', |
| 677 | 'bookwithoptionalisbn_set-1-title': 'Fokke en Sukke', |
| 678 | } |
| 679 | formset = AuthorBooksFormSet(data, instance=author) |
| 680 | self.assertTrue(formset.is_valid(), formset.errors) |
| 681 | |
| 682 | saved = formset.save() |
| 683 | self.assertEqual(len(saved), 2) |
| 684 | book1, book2 = saved |
| 685 | self.assertEqual(book1.author_id, 1) |
| 686 | self.assertEqual(book1.title, 'Het gouden ei') |
| 687 | self.assertEqual(book1.priority, None) |
| 688 | self.assertEqual(book2.author_id, 1) |
| 689 | self.assertEqual(book2.title, 'Fokke en Sukke') |
| 690 | self.assertEqual(book2.priority, None) |
| 691 | |