在父模型的详细信息页面中显示内联模型?烧瓶,SQLAlchemy
Display inline model in the details page of the parent model? Flask, SQLAlchemy
我有一个模型 A,它包含一个允许用户输入一些文本的内联模型 B。目前,添加到内联模型中的数据用户只能在父模型A的'edit'页面中显示,而不会在'details'页面中显示。有办法解决吗?
编辑页面
将模型 B 中的字段添加到 column_details_list
(docs)
将模型 B 中的相同字段添加到 column_formatters_detail
字典 (docs),指定 returns 适合 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
}
我有一个模型 A,它包含一个允许用户输入一些文本的内联模型 B。目前,添加到内联模型中的数据用户只能在父模型A的'edit'页面中显示,而不会在'details'页面中显示。有办法解决吗?
编辑页面
将模型 B 中的字段添加到 column_details_list
(docs)
将模型 B 中的相同字段添加到 column_formatters_detail
字典 (docs),指定 returns 适合 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
}