【问题标题】:Display inline model in the details page of the parent model? Flask, SQLAlchemy在父模型的详细信息页面中显示内联模型?烧瓶,SQLAlchemy
【发布时间】:2021-04-12 00:47:58
【问题描述】:

我有一个模型 A,它包含一个允许用户输入一些文本的内联模型 B。目前,添加到内联模型中的数据用户只能显示在父模型A的“编辑”页面中,而不能显示在“详细信息”页面中。有没有办法解决这个问题?

编辑页面

【问题讨论】:

    标签: flask sqlalchemy flask-sqlalchemy flask-wtforms flask-admin


    【解决方案1】:

    将模型B中的字段添加到column_details_listdocs

    将模型 B 中的相同字段添加到 column_formatters_detail 字典 (docs),指定返回适当 HTML 的格式化程序方法。

    例如:

    from markupsafe import Markup
    
    class ExampleView(AdminView):
    
        # include the comments child field plus any parent fields from model A you want to show
        column_details_list =  ('name', 'last_name', 'comments')    
    
        def _comments_formatter(view, context, model, name):
            # model is parent model A
            _html = []
            if model.comments:
                #  return any valid HTML markup
                for _comment_model in model.comments:
                    # add html para per comment
                    _html.append(f'<p>User:{str(_comment_model.user)}, Comment:{_comment_model.comment}</p>')
    
                return Markup(''.join(_html))
    
        column_formatters_detail = {
            'comments': _comments_formatter
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2020-12-06
      • 2015-12-14
      • 2020-02-02
      • 2020-06-30
      • 2016-07-10
      • 1970-01-01
      相关资源
      最近更新 更多