【问题标题】:Flask appbuilder with custom list and search具有自定义列表和搜索功能的 Flask AppBuilder
【发布时间】:2022-12-14 10:41:30
【问题描述】:

我是 flask 的新用户,我想避免 flask appbuilder 使用“列表”填充整个模型数据集,而不是仅根据搜索过滤器条目进行填充。有没有办法使这种默认行为。我看过 active_filter 的选项但不确定如何覆盖。我检查了 base_filter 但这一直都在应用。

此外,我还注意到搜索表单可以覆盖传递 WTF 表单的任何指针,其中包含列的允许值列表。

任何帮助是极大的赞赏。谢谢你。

【问题讨论】:

    标签: flask flask-wtforms flask-appbuilder


    【解决方案1】:

    下面是一个示例,说明如何避免使用整个模型数据集填充 Flask appbuilder

    # First, create a function that filters the data based on the search query
    def filter_data(query):
      # Your code here
    
    # Next, use the active_filter option to apply the filter function to the appbuilder
    appbuilder = FlaskAppBuilder(app, db.session, active_filter=filter_data)
    
    # Finally, you can use the base_filter option to apply a default filter
    # to the appbuilder, if you want to
    appbuilder = FlaskAppBuilder(app, db.session, base_filter=my_default_filter)
    

    至于覆盖搜索表单,您可以使用 WTForms 库创建一个自定义表单,其中包含列的允许值列表。

    这是一个例子:

    # Import the necessary classes from WTForms
    from wtforms import Form, StringField
    
    # Create a custom form class
    class MyCustomSearchForm(Form):
      # Create a StringField with a list of allowed values
      search_query = StringField(choices=['allowed value 1', 'allowed value 2', ...])
    
    # Use the custom form when creating the appbuilder
    appbuilder = FlaskAppBuilder(app, db.session, search_form=MyCustomSearchForm)
    

    我希望这有帮助!如果您有任何问题,请告诉我。祝你的 Flask 之旅好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 2018-07-30
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      相关资源
      最近更新 更多