Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

python - Flask-Admin default filters

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...