Forked from riteshreddyr/flask-admin many-to-many search.md
Created
September 2, 2016 13:31
-
-
Save 8dspaces/a3ed6c5069a7801dccc6856d975ad574 to your computer and use it in GitHub Desktop.
Revisions
-
riteshreddyr revised this gist
May 24, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ Many-to-Many searches are not easily supported by Flask-admin. [Read Here for more info ](http://codeseekah.com/2013/08/04/flask-admin-hacks-for-many-to-many-relationships/) ##Solution * Add the association tables to the list of join tables so that the join between the two tables can be made through the association table. * Reverse the tables to ensure that the association tables are processed before the other join table. Tested on Flask-Admin 1.1.0 -
riteshreddyr revised this gist
May 24, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,5 @@ #Flask-admin Many-to-Many Search Many-to-Many searches are not easily supported by Flask-admin. [Read Here for more info ](http://codeseekah.com/2013/08/04/flask-admin-hacks-for-many-to-many-relationships/) ##Solution -
riteshreddyr revised this gist
May 24, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -47,7 +47,7 @@ taxonomy = Table( #Check if "tags" table has been processed and the join table added if "tags" in self._filter_joins: #add the association table to the filter join tables self._filter_joins['tags'].append(taxonomy) #reverse the list so that the association table appears before the two main tables self._filter_joins['tags'].reverse() -
riteshreddyr revised this gist
May 24, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -47,9 +47,9 @@ taxonomy = Table( #Check if "tags" table has been processed and the join table added if "tags" in self._filter_joins: #add the association table to the filter join tables self._filter_joins['tags'](taxonomy) #reverse the list so that the association table appears before the two main tables self._filter_joins['tags'].reverse() return filters -
riteshreddyr revised this gist
May 24, 2015 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,11 @@ #Flask-admin Many-to-Many Search Setting up Flask-admin with SQLAlchemy to be able to search through many-to-many relationships Many-to-Many searches are not easily supported by Flask-admin. [Read Here for more info ](http://codeseekah.com/2013/08/04/flask-admin-hacks-for-many-to-many-relationships/) ##Solution * Add the association tables to the list of join tables so that the join between the three tables can be successful. * Reverse the tables to ensure that the association tables are processed before the other join table. Tested on Flask-Admin 1.1.0 -
riteshreddyr renamed this gist
May 24, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
riteshreddyr created this gist
May 24, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ #Setting up Flask-admin with SQLAlchemy to be able to search through many-to-many relationships Many-to-Many searches are not easily supported by Flask-admin. [Read Here for more info ](http://codeseekah.com/2013/08/04/flask-admin-hacks-for-many-to-many-relationships/) In order to allow for the many-to-many search/filtering support, the association tables need to be added to the list of join tables and then the order of these tables reversed. Tested on Flask-Admin 1.1.0 ``` class Post( Base ): __tablename__ = 'posts' id = Column( Integer, primary_key=True ) title = Column( Unicode( 255 ), nullable=False ) tags = relationship( 'Tag', backref='posts', secondary='taxonomy' ) class Tag( Base ): __tablename__ = 'tags' id = Column( Integer, primary_key=True ) name = Column( Unicode( 255 ), nullable=False ) taxonomy = Table( 'taxonomy', Base.metadata, Column( 'post_id', Integer, ForeignKey( 'posts.id' ) ), Column( 'tag_id', Integer, ForeignKey( 'tags.id' ) ), ) ``` ``` class PostModelView( ModelView ): column_searchable_list = ( Post.title, Tag.name ) column_filters = (Post.title, Tag.name) def init_search( self ): r = super( PostModelView, self ).init_search() #add the association table to search join list self._search_joins.append(taxonomy) #reverse the lsit so that the association table appears before the two main tables self._search_joins.reverse() return r def scaffold_filters(self, name): filters = super( PostModeView, self).scaffold_filters(name) #Check if "tags" table has been processed and the join table added if "tags" in self._filter_joins: #add the association table to the filter join tables self._filter_joins.append(taxonomy) #reverse the list so that the association table appears before the two main tables self._filter_joins.reverse() return filters ```