python – 在Flask-admin ModelView中对自定义字段进行筛选和排序

我想在模型视图中添加金额(自定义字段)的过滤器

class FarmerAdmin(ModelView):

    column_descriptions = dict(amount = "All Transactions Total (Rs.)")

    column_searchable_list = ('name','village','phone')

    column_editable_list = ('phone',)

    column_filters = ('village','area',\
                  filters.FloatGreaterFilter('amount','Amount'))

    inline_models = (Transaction,)

    def get_query(self):
        return self.session.query(
            Farmer.id.label("id"),
            Farmer.name.label("name"),
            Farmer.village.label("village"),
            Farmer.phone.label("phone"),
            Farmer.area.label("area"),
            func.sum(Transaction.total).label("amount")
            ).join(Transaction).group_by(Farmer.id)

    def scaffold_sortable_columns(self):
        return {'id':'id','name':'name','village':'village',\
            'area':'area','amount':'amount'}

    def scaffold_list_columns(self):
        return ['id','name','village','phone','area','amount']


    def __init__(self, session):
        super(FarmerAdmin, self).__init__(Farmer, session)

通过重写scaffold_sortable_columns方法,排序很有效.有没有办法为量列添加标准过滤器(大于,小于).

最佳答案
我会以更大的为例

from flask_admin.contrib.sqla.filters import FilterGreater

class FarmerAdmin(ModelView):
    FilterGreater(Farmer.amount, u'Amount')

有关更多信息,请查看http://flask-admin.readthedocs.io/en/latest/_modules/flask_admin/contrib/sqla/filters/

转载注明原文:python – 在Flask-admin ModelView中对自定义字段进行筛选和排序 - 代码日志