| | 38 | class Editor(models.Model): |
| | 39 | name = models.CharField(max_length=100) |
| | 40 | |
| | 41 | class BookWithOptionalAltEditor(models.Model): |
| | 42 | author = models.ForeignKey(Author) |
| | 43 | # Optional secondary author |
| | 44 | alt_editor = models.ForeignKey(Editor, blank=True, null=True) |
| | 45 | title = models.CharField(max_length=100) |
| | 46 | |
| | 47 | class Meta: |
| | 48 | unique_together = ( |
| | 49 | ('author', 'title', 'alt_editor'), |
| | 50 | ) |
| | 51 | |
| | 52 | def __unicode__(self): |
| | 53 | return self.title |
| | 54 | |
| | 670 | >>> AuthorBooksFormSet4 = inlineformset_factory(Author, BookWithOptionalAltEditor, can_delete=False, extra=2) |
| | 671 | |
| | 672 | >>> formset = AuthorBooksFormSet4(instance=author) |
| | 673 | >>> for form in formset.forms: |
| | 674 | ... print form.as_p() |
| | 675 | <p><label for="id_bookwithoptionalalteditor_set-0-alt_editor">Alt editor:</label> <select name="bookwithoptionalalteditor_set-0-alt_editor" id="id_bookwithoptionalalteditor_set-0-alt_editor"> |
| | 676 | <option value="" selected="selected">---------</option> |
| | 677 | </select></p> |
| | 678 | <p><label for="id_bookwithoptionalalteditor_set-0-title">Title:</label> <input id="id_bookwithoptionalalteditor_set-0-title" type="text" name="bookwithoptionalalteditor_set-0-title" maxlength="100" /><input type="hidden" name="bookwithoptionalalteditor_set-0-author" value="1" id="id_bookwithoptionalalteditor_set-0-author" /><input type="hidden" name="bookwithoptionalalteditor_set-0-id" id="id_bookwithoptionalalteditor_set-0-id" /></p> |
| | 679 | <p><label for="id_bookwithoptionalalteditor_set-1-alt_editor">Alt editor:</label> <select name="bookwithoptionalalteditor_set-1-alt_editor" id="id_bookwithoptionalalteditor_set-1-alt_editor"> |
| | 680 | <option value="" selected="selected">---------</option> |
| | 681 | </select></p> |
| | 682 | <p><label for="id_bookwithoptionalalteditor_set-1-title">Title:</label> <input id="id_bookwithoptionalalteditor_set-1-title" type="text" name="bookwithoptionalalteditor_set-1-title" maxlength="100" /><input type="hidden" name="bookwithoptionalalteditor_set-1-author" value="1" id="id_bookwithoptionalalteditor_set-1-author" /><input type="hidden" name="bookwithoptionalalteditor_set-1-id" id="id_bookwithoptionalalteditor_set-1-id" /></p> |
| | 683 | |
| | 684 | >>> data = { |
| | 685 | ... 'bookwithoptionalalteditor_set-TOTAL_FORMS': '2', # the number of forms rendered |
| | 686 | ... 'bookwithoptionalalteditor_set-INITIAL_FORMS': '0', # the number of forms with initial data |
| | 687 | ... 'bookwithoptionalalteditor_set-MAX_NUM_FORMS': '', # the max number of forms |
| | 688 | ... 'bookwithoptionalalteditor_set-0-author': '1', |
| | 689 | ... 'bookwithoptionalalteditor_set-0-title': 'Les Fleurs du Mal', |
| | 690 | ... 'bookwithoptionalalteditor_set-1-author': '1', |
| | 691 | ... 'bookwithoptionalalteditor_set-1-title': 'Les Fleurs du Mal', |
| | 692 | ... } |
| | 693 | |
| | 694 | >>> formset = AuthorBooksFormSet4(data, instance=author) |
| | 695 | >>> formset.is_valid() |
| | 696 | True |
| | 697 | |
| | 698 | >>> formset.save() |
| | 699 | [<BookWithOptionalAltEditor: Les Fleurs du Mal>, <BookWithOptionalAltEditor: Les Fleurs du Mal>] |
| | 700 | |
| | 701 | |