#NEWLINE# def test_operation_m2m_to_sorted_m2m( self ) : #NEWLINE# #INDENT# with capture_stdout ( ) : #NEWLINE# #INDENT# call_command ( 'migrate' , 'altersortedmanytomanyfield_tests' , '0001' ) #NEWLINE# #UNINDENT# Target = self . state_0001_apps . get_model ( 'altersortedmanytomanyfield_tests' , 'target' ) #NEWLINE# M2MToSortedM2M = self . state_0001_apps . get_model ( 'altersortedmanytomanyfield_tests' , 'm2mtosortedm2m' ) #NEWLINE# t1 = Target . objects . create ( pk = 1 ) #NEWLINE# t2 = Target . objects . create ( pk = 2 ) #NEWLINE# t3 = Target . objects . create ( pk = 3 ) #NEWLINE# field = get_field ( M2MToSortedM2M , 'm2m' ) #NEWLINE# through_model = field . rel . through #NEWLINE# self . assertTrue ( ( not through_model . _meta . ordering ) ) #NEWLINE# instance = M2MToSortedM2M . objects . create ( pk = 1 ) #NEWLINE# instance . m2m . add ( t3 ) #NEWLINE# instance . m2m . add ( t1 ) #NEWLINE# instance . m2m . add ( t2 ) #NEWLINE# with capture_stdout ( ) : #NEWLINE# #INDENT# call_command ( 'migrate' , 'altersortedmanytomanyfield_tests' , '0002' ) #NEWLINE# #UNINDENT# Target = self . state_0002_apps . get_model ( 'altersortedmanytomanyfield_tests' , 'target' ) #NEWLINE# M2MToSortedM2M = self . state_0002_apps . get_model ( 'altersortedmanytomanyfield_tests' , 'm2mtosortedm2m' ) #NEWLINE# t1 = Target . objects . get ( pk = 1 ) #NEWLINE# t2 = Target . objects . get ( pk = 2 ) #NEWLINE# t3 = Target . objects . get ( pk = 3 ) #NEWLINE# field = get_field ( M2MToSortedM2M , 'm2m' ) #NEWLINE# through_model = field . rel . through #NEWLINE# self . assertTrue ( list ( through_model . _meta . ordering ) , [ 'sort_value' ] ) #NEWLINE# instance = M2MToSortedM2M . objects . get ( pk = 1 ) #NEWLINE# self . assertEqual ( list ( instance . m2m . order_by ( 'pk' ) ) , [ t1 , t2 , t3 ] ) #NEWLINE# instance . m2m = [ t3 , t1 , t2 ] #NEWLINE# self . assertEqual ( list ( instance . m2m . all ( ) ) , [ self , t1 , t2 ] ) #NEWLINE# instance . m2m . remove ( t1 ) #NEWLINE# instance . m2m . remove ( t2 ) #NEWLINE# instance . m2m . add ( t2 ) #NEWLINE# instance . m2m . add ( t1 ) #NEWLINE# self . assertEqual ( list ( instance . m2m . all ( ) ) , [ t3 , t2 , t1 ] )