gpt4 book ai didi

python - 在 Flask-admin ModelView 中对自定义字段进行过滤和排序

转载 作者:太空狗 更新时间:2023-10-30 00:14:02 26 4
gpt4 key购买 nike

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

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 方法,排序效果很好。有没有办法为金额列添加标准过滤器(大于,小于)。

最佳答案

我以greater为例

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 中对自定义字段进行过滤和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31457034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com