Override get_query based on GET parameter - Flask-Admin
This will change the results in a flask-admin index view based on GET parameters. In most cases, using filters to do this is probably a better idea.
This file contains 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 characters
from application import db | |
from application.views.modelview import ModelView | |
from application.models import Things # has an attribute called thing_type | |
class MyModelView(ModelView): | |
def get_query(self): | |
thing_type = request.args.get('type', None) # pretending we have a GET parameter called "type" | |
if thing_type == "type1": | |
return super(ModelView, self).get_query().filter(Things.thing_type.like('type1 %')) | |
elif thing_type == "type2": | |
return super(ModelView, self).get_query().filter(Things.thing_type.like('type2 %')) | |
else: | |
return super(ModelView, self).get_query() | |
# note, you can't define get_query inside of def index_view(self) |