Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Admin default filters

Tags:

I would like to display only paid orders in my Flask-Admin model list view.

Here is models.py:

class Order(db.Model):    id = db.Column(db.Integer, primary_key=True)    amount = db.Column(db.Integer)    description = db.Column(db.String)    paid = db.Column(db.Boolean, default=False) 

Here is ModelView for Flask-Admin:

class OrderView(ModelView):     column_filters = ("paid")   admin.add_view(OrderView(Order, db.session)) 

Filters work fine, but I would like to make this filter default. Or better yet, do not use filters, and only show orders that are output of Order.query.filter(Order.paid==True) query.

Is it possible to do with Flask-Admin?

like image 933
user2672932 Avatar asked Oct 13 '14 22:10

user2672932


1 Answers

We do this in our app by overriding ModelView.

https://github.com/mrjoes/flask-admin/blob/master/flask_admin/contrib/sqla/view.py#L654

I looked through the source code a bit for Flask-Admin, and they've made the API easier to use since we last edited this code because it looks like you can just do:

from flask.ext.admin.contrib.sqla.view import ModelView, func  class PaidOrderView(ModelVew):     def get_query(self):       return self.session.query(self.model).filter(self.model.paid==True)      def get_count_query(self):       return self.session.query(func.count('*')).filter(self.model.paid==True) 

(We were overriding get_list() which is not nearly as great.)

You can then use it like:

admin.add_view(PaidOrderView(Order, db.session)) 

Let me know if that doesn't work for you and I can take another look.

like image 197
Rachel Sanders Avatar answered Sep 18 '22 14:09

Rachel Sanders