firkraag firkraag - 1 year ago 185
Python Question

Flask-Admin: route to models view with filter applied

I have a model in Flask-Admin with filter (e.g. based on Foreign Key to other model).

I want to generate links from front-end to this model view in admin with filter value applied. I noticed that it adds

to the
, so that the whole address looks kinda:

http:/.../admin/model_view_<my model>/?flt0_0=<filter value>

Which is the best way to generate routes like this?

Answer Source

Unfortunately, there's no public API for this yet. Here's a short snippet you can use for now to generate fltX_Y query string:

class MyView(BaseModelView):
    def get_filter_arg(self, filter_name, filter_op='equals'):
        filters = self._filter_groups[filter_name].filters
        position = self._filter_groups.keys().index(filter_name)

        for f in filters:
            if f['operation'] == filter_op:
                return 'flt%d_%d' % (position, f['index'])

Then you can call this method on a your view instance:

print my_view.get_filter_arg('Name', 'contains')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download