如何使用Flask-Admin ModelView过滤编辑窗体中的列? [英] How can I filter a column in the edit form with Flask-Admin ModelView?

查看:474
本文介绍了如何使用Flask-Admin ModelView过滤编辑窗体中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用flask-admin使用模型视图,我想在编辑/创建视图中过滤一列。列/字段是一种关系,我只想显示属于已登录用户的字段,即 relationship_id == user.id


<事实上,我发现一个更简单的方法如下,似乎没有必要重写ModelView中的edit_form方法,只需将过滤函数作为命名参数(query_factory )到form_args,它就像一个迷人的!

  class CustomModelView(ModelView):
form_args = dict (
status = dict(label ='Status',query_factory = filtering_function)

$ b $ def filter_function():
返回app.db.query(CustomModel) .filter_by(field_to_filter = my_criteria)


I am using a model view with flask-admin and I want to filter a column in the edit/create view. The column/field is a relationship and I only want to show fields that belong to the logged in user i.e. relationship_id == user.id

解决方案

Actually I found a easier way for that as below, seems there's no need to override the edit_form method in ModelView, just pass the filtering function as a named parameter(query_factory) to the form_args, and it works like a charming!.

class CustomModelView(ModelView):
    form_args = dict(
        status = dict(label='Status', query_factory=filtering_function)
    )

def filtering_function():
   return app.db.query(CustomModel).filter_by(field_to_filter=my_criteria)

这篇关于如何使用Flask-Admin ModelView过滤编辑窗体中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆