使用flask_sqlalchemy
,我有两个表(A和B(通过一个关联代理表(C(连接在一起,其中有一些额外的字段。
现在有了flask_admin我有一个关于 C 的ModelView
,在这个列表视图中,我已经(使用column_formatters
选项(在相关 A 表列中的每个项目上附加了一个链接到其details_view页面。
它工作正常,但我不知道,在这个详细信息视图页面中,如何附加一个基于 C 的filtered
表,只包含与所选 A 项相关的行。
好的,如果有人需要,我会在这里回答自己。
我通过创建另一个list_view来做相反的事情,但对于选定的记录,我希望通过修改 index_view 方法并将额外的参数(详细信息(传递给模板:
class TableCView(admin.ModelView):
list_template = 'modified_listview.html'
@expose('/')
def index_view(self):
self.a_id = request.args.get('id', None)
self._template_args['a_id'] = self.a_id # add more params here
return super(TableCView, self).index_view()
def get_query(self):
# Filter records by a_id
query = super(TableCView, self).get_query()
if self.a_id:
query = query.filter(self.model.a_id == self.a_id)
return query
def get_count_query(self):
query = super(TableCView, self).get_count_query()
if self.a_id:
query = query.filter(self.model.a_id == self.a_id)
return query