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

问题描述

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


  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过滤编辑窗体中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 07:29