#NEWLINE# def test_ajax_fk_multi( ) : #NEWLINE# #INDENT# ( app , db , admin ) = setup ( ) #NEWLINE# class Model1 ( db . Model ) : #NEWLINE# #INDENT# __tablename__ = 'model1' #NEWLINE# id = db . Column ( db . Integer , primary_key = True ) #NEWLINE# name = db . Column ( db . String ( 20 ) ) #NEWLINE# def __str__( self ) : #NEWLINE# #INDENT# return self . name #NEWLINE# #UNINDENT# #UNINDENT# table = db . Table ( 'm2m' , db . Model . metadata , db . Column ( 'model1_id' , db . Integer , db . ForeignKey ( 'model1.id' ) ) , db . Column ( 'model2_id' , db . Integer , db . ForeignKey ( 'model2.id' ) ) ) #NEWLINE# class Model2 ( db . Model ) : #NEWLINE# #INDENT# __tablename__ = 'model2' #NEWLINE# id = db . Column ( db . Integer , primary_key = True ) #NEWLINE# name = db . Column ( db . String ( 20 ) ) #NEWLINE# model1_id = db . Column ( db . Integer ( ) , db . ForeignKey ( Model1 . id ) ) #NEWLINE# model1 = db . relationship ( Model1 , backref = 'models2' , secondary = table ) #NEWLINE# #UNINDENT# db . create_all ( ) #NEWLINE# view = CustomModelView ( Model2 , db . session , url = 'view' , form_ajax_refs = { #NEWLINE# #INDENT# 'model1' : { #NEWLINE# #INDENT# 'fields' : [ 'name' ] , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } ) #NEWLINE# admin . add_view ( view ) #NEWLINE# ok_ ( ( 'model1' in view . _form_ajax_refs ) ) #NEWLINE# model = Model1 ( name = 'first' ) #NEWLINE# db . session . add_all ( [ model , Model1 ( name = 'foo' ) ] ) #NEWLINE# db . session . commit ( ) #NEWLINE# form = view . create_form ( ) #NEWLINE# eq_ ( form . model1 . __class__ . __name__ , 'AjaxSelectMultipleField' ) #NEWLINE# with app . test_request_context ( '/admin/view/' ) : #NEWLINE# #INDENT# ok_ ( ( 'data-json="[]"' in form . model1 ( ) ) ) #NEWLINE# form . model1 . data = [ model ] #NEWLINE# ok_ ( ( 'data-json="[[1, &quot;first&quot;]]"' in form . model1 ( ) ) ) #NEWLINE# #UNINDENT# client = app . test_client ( ) #NEWLINE# client . post ( '/admin/view/new/' , data = { #NEWLINE# #INDENT# 'model1' : as_unicode ( admin . id ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# mdl = db . session . query ( Model2 ) . first ( ) #NEWLINE# ok_ ( ( mdl is not None ) ) #NEWLINE# ok_ ( ( mdl . model1 is not None ) ) #NEWLINE# eq_ ( len ( mdl . model1 ) , 1 )