我有一个模型A,它包含一个内联模型B,允许用户输入一些文本。目前,添加到内联模型中的数据用户只能在父模型A的“编辑”页面中显示,而不能在“详细信息”页面中显示。有没有办法解决这个问题?
编辑-页面
发布于 2021-01-06 16:14:29
将模型B中的字段添加到column_details_list
(docs)
将模型B中的相同字段添加到HTML字典(docs)中,指定一个返回适当column_formatters_detail
的格式化程序方法。
例如:
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
}
https://stackoverflow.com/questions/65592955
复制