#NEWLINE# def test_inline_formsets( self ) : #NEWLINE# #INDENT# AuthorBooksFormSet = inlineformset_factory ( Author , Book , can_delete = False , extra = 3 ) #NEWLINE# author = Author . objects . create ( name = 'Charles Baudelaire' ) #NEWLINE# formset = AuthorBooksFormSet ( instance = author ) #NEWLINE# self . assertEqual ( len ( formset . forms ) , 3 ) #NEWLINE# self . assertHTMLEqual ( formset . forms [ 0 ] . as_p ( ) , ( '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" maxlength="100" /><input type="hidden" name="book_set-0-author" value="%d" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" id="id_book_set-0-id" /></p>' % author . id ) ) #NEWLINE# self . assertHTMLEqual ( formset . forms [ 1 ] . as_p ( ) , ( '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="%d" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>' % author . id ) ) #NEWLINE# self . assertHTMLEqual ( formset . forms [ 2 ] . as_p ( ) , ( '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="%d" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>' % author . id ) ) #NEWLINE# data = { #NEWLINE# #INDENT# 'book_set-TOTAL_FORMS' : '3' , #NEWLINE# 'book_set-INITIAL_FORMS' : '0' , #NEWLINE# 'book_set-MAX_NUM_FORMS' : '' , #NEWLINE# 'book_set-0-title' : 'Les Fleurs du Mal' , #NEWLINE# 'book_set-1-title' : '' , #NEWLINE# 'book_set-2-title' : '' , #NEWLINE# #UNINDENT# } #NEWLINE# formset = AuthorBooksFormSet ( data , instance = author ) #NEWLINE# self . assertTrue ( formset . is_valid ( ) ) #NEWLINE# saved = formset . save ( ) #NEWLINE# self . assertEqual ( len ( saved ) , 1 ) #NEWLINE# ( book1 , ) = saved #NEWLINE# self . assertEqual ( book1 , Book . objects . get ( title = 'Les Fleurs du Mal' ) ) #NEWLINE# self . assertQuerysetEqual ( author . book_set . all ( ) , [ '<Book: Les Fleurs du Mal>' ] ) #NEWLINE# AuthorBooksFormSet = inlineformset_factory ( Author , Book , can_delete = False , extra = 2 ) #NEWLINE# author = Author . objects . get ( name = 'Charles Baudelaire' ) #NEWLINE# formset = AuthorBooksFormSet ( instance = AuthorBooksFormSet ) #NEWLINE# self . assertEqual ( len ( formset . forms ) , 3 ) #NEWLINE# self . assertHTMLEqual ( formset . forms [ 0 ] . as_p ( ) , ( '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-0-author" value="%d" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="%d" id="id_book_set-0-id" /></p>' % ( author . id , book1 . id ) ) ) #NEWLINE# self . assertHTMLEqual ( formset . forms [ 1 ] . as_p ( ) , ( '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="%d" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>' % author . id ) ) #NEWLINE# self . assertHTMLEqual ( formset . forms [ 2 ] . as_p ( ) , ( '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="%d" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>' % author . id ) ) #NEWLINE# data = { #NEWLINE# #INDENT# 'book_set-TOTAL_FORMS' : '3' , #NEWLINE# 'book_set-INITIAL_FORMS' : '1' , #NEWLINE# 'book_set-MAX_NUM_FORMS' : '' , #NEWLINE# 'book_set-0-id' : str ( book1 . id ) , #NEWLINE# 'book_set-0-title' : 'Les Fleurs du Mal' , #NEWLINE# 'book_set-1-title' : 'Les Paradis Artificiels' , #NEWLINE# 'book_set-2-title' : '' , #NEWLINE# #UNINDENT# } #NEWLINE# formset = AuthorBooksFormSet ( data , instance = author ) #NEWLINE# self . assertTrue ( formset . is_valid ( ) ) #NEWLINE# saved = formset . save ( ) #NEWLINE# self . assertEqual ( len ( saved ) , 1 ) #NEWLINE# ( book2 , ) = saved #NEWLINE# self . assertEqual ( book2 , Book . objects . get ( title = 'Les Paradis Artificiels' ) ) #NEWLINE# self . assertQuerysetEqual ( author . book_set . order_by ( 'title' ) , [ '<Book: Les Fleurs du Mal>' , '<Book: Les Paradis Artificiels>' ] )