#NEWLINE# def test_inline_formsets_with_custom_save_method( self ) : #NEWLINE# #INDENT# AuthorBooksFormSet = inlineformset_factory ( Author , Book , can_delete = False , extra = 2 ) #NEWLINE# author = Author . objects . create ( pk = 1 , name = 'Charles Baudelaire' ) #NEWLINE# book1 = Book . objects . create ( pk = 1 , author = author , title = 'Les Paradis Artificiels' ) #NEWLINE# book2 = Book . objects . create ( pk = 2 , author = author , title = 'Les Fleurs du Mal' ) #NEWLINE# book3 = Book . objects . create ( pk = 3 , author = author , title = 'Flowers of Evil' ) #NEWLINE# class PoemForm ( forms . ModelForm ) : #NEWLINE# #INDENT# def save( self , commit = True ) : #NEWLINE# #INDENT# poem = super ( PoemForm , self ) . save ( commit = False ) #NEWLINE# poem . name = 'Brooklyn Bridge' #NEWLINE# if commit : #NEWLINE# #INDENT# poem . save ( ) #NEWLINE# #UNINDENT# return poem #NEWLINE# #UNINDENT# #UNINDENT# PoemFormSet = inlineformset_factory ( Poet , Poem , form = PoemForm ) #NEWLINE# data = { #NEWLINE# #INDENT# 'poem_set-TOTAL_FORMS' : '3' , #NEWLINE# 'poem_set-INITIAL_FORMS' : '0' , #NEWLINE# 'poem_set-MAX_NUM_FORMS' : '' , #NEWLINE# 'poem_set-0-name' : 'The Cloud in Trousers' , #NEWLINE# 'poem_set-1-name' : 'I' , #NEWLINE# 'poem_set-2-name' : '' , #NEWLINE# #UNINDENT# } #NEWLINE# poet = Poet . objects . create ( name = 'Vladimir Mayakovsky' ) #NEWLINE# formset = PoemFormSet ( data = data , instance = poet ) #NEWLINE# self . assertTrue ( formset . is_valid ( ) ) #NEWLINE# saved = formset . save ( ) #NEWLINE# self . assertEqual ( len ( saved ) , 2 ) #NEWLINE# ( poem1 , poem2 ) = saved #NEWLINE# self . assertEqual ( poem1 . name , 'Brooklyn Bridge' ) #NEWLINE# self . assertEqual ( poem2 . name , 'Brooklyn Bridge' ) #NEWLINE# custom_qs = Book . objects . order_by ( '-title' ) #NEWLINE# formset = AuthorBooksFormSet ( instance = author , queryset = custom_qs ) #NEWLINE# self . assertEqual ( len ( formset . forms ) , 5 ) #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 Paradis Artificiels" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="1" id="id_book_set-0-id" /></p>' ) #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" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" value="2" id="id_book_set-1-id" /></p>' ) #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" value="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" value="3" id="id_book_set-2-id" /></p>' ) #NEWLINE# self . assertHTMLEqual ( formset . forms [ 3 ] . as_p ( ) , '<p><label for="id_book_set-3-title">Title:</label> <input id="id_book_set-3-title" type="text" name="book_set-3-title" maxlength="100" /><input type="hidden" name="book_set-3-author" value="1" id="id_book_set-3-author" /><input type="hidden" name="book_set-3-id" id="id_book_set-3-id" /></p>' ) #NEWLINE# self . assertHTMLEqual ( formset . forms [ 4 ] . as_p ( ) , '<p><label for="id_book_set-4-title">Title:</label> <input id="id_book_set-4-title" type="text" name="book_set-4-title" maxlength="100" /><input type="hidden" name="book_set-4-author" value="1" id="id_book_set-4-author" /><input type="hidden" name="book_set-4-id" id="id_book_set-4-id" /></p>' ) #NEWLINE# data = { #NEWLINE# #INDENT# 'book_set-TOTAL_FORMS' : '5' , #NEWLINE# 'book_set-INITIAL_FORMS' : '3' , #NEWLINE# 'book_set-MAX_NUM_FORMS' : '' , #NEWLINE# 'book_set-0-id' : str ( book1 . id ) , #NEWLINE# 'book_set-0-title' : 'Les Paradis Artificiels' , #NEWLINE# 'book_set-1-id' : str ( book2 . id ) , #NEWLINE# 'book_set-1-title' : 'Les Fleurs du Mal' , #NEWLINE# 'book_set-2-id' : str ( book3 . id ) , #NEWLINE# 'book_set-2-title' : 'Flowers of Evil' , #NEWLINE# 'book_set-3-title' : 'Revue des deux mondes' , #NEWLINE# 'book_set-4-title' : '' , #NEWLINE# #UNINDENT# } #NEWLINE# formset = AuthorBooksFormSet ( data , instance = author , queryset = custom_qs ) #NEWLINE# self . assertTrue ( formset . is_valid ( ) ) #NEWLINE# custom_qs = Book . objects . filter ( title__startswith = 'F' ) #NEWLINE# formset = AuthorBooksFormSet ( instance = author , queryset = custom_qs ) #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="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="3" id="id_book_set-0-id" /></p>' ) #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="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>' ) #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="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>' ) #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 ( book3 . id ) , #NEWLINE# 'book_set-0-title' : 'Flowers of Evil' , #NEWLINE# 'book_set-1-title' : 'Revue des deux mondes' , #NEWLINE# 'book_set-2-title' : '' , #NEWLINE# #UNINDENT# } #NEWLINE# formset = AuthorBooksFormSet ( data , instance = author , queryset = custom_qs ) #NEWLINE# self . assertTrue ( formset . is_valid ( ) )