Skip to content

Instantly share code, notes, and snippets.

@pawl
Created September 10, 2014 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawl/5fcd1d206c874d091302 to your computer and use it in GitHub Desktop.
Save pawl/5fcd1d206c874d091302 to your computer and use it in GitHub Desktop.
Override get_query based on GET parameter - Flask-Admin
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment