Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 8dspaces/a3ed6c5069a7801dccc6856d975ad574 to your computer and use it in GitHub Desktop.
Save 8dspaces/a3ed6c5069a7801dccc6856d975ad574 to your computer and use it in GitHub Desktop.

Revisions

  1. @riteshreddyr riteshreddyr revised this gist May 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion flask-admin many-to-many search.md
    Original 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 three tables can be successful.
    * 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
  2. @riteshreddyr riteshreddyr revised this gist May 24, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion flask-admin many-to-many search.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #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
  3. @riteshreddyr riteshreddyr revised this gist May 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion flask-admin many-to-many search.md
    Original 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'](taxonomy)
    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()
  4. @riteshreddyr riteshreddyr revised this gist May 24, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions flask-admin many-to-many search.md
    Original 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.append(taxonomy)
    self._filter_joins['tags'](taxonomy)
    #reverse the list so that the association table appears before the two main tables
    self._filter_joins.reverse()
    self._filter_joins['tags'].reverse()
    return filters
  5. @riteshreddyr riteshreddyr revised this gist May 24, 2015. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions flask-admin many-to-many search.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,11 @@
    #Setting up Flask-admin with SQLAlchemy to be able to search through many-to-many relationships
    #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/)

    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.
    ##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

  6. @riteshreddyr riteshreddyr renamed this gist May 24, 2015. 1 changed file with 0 additions and 0 deletions.
  7. @riteshreddyr riteshreddyr created this gist May 24, 2015.
    52 changes: 52 additions & 0 deletions flask-admin many-to-many search
    Original 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

    ```