Python Flask管理模型-摘要行
我正在使用Flask Admin(),我有一个疑问 假设我有一个模型: 产品 序列号 价格 我正在寻找一种方法来显示一个新的行,该行将显示库存产品的总价格 有人能给我指一下正确的方向吗 多谢各位 我已经对此进行了研究,但当我显示自定义视图时,我不能像在“标准”模型视图上那样执行CRUD操作:Python Flask管理模型-摘要行,python,flask-sqlalchemy,flask-admin,Python,Flask Sqlalchemy,Flask Admin,我正在使用Flask Admin(),我有一个疑问 假设我有一个模型: 产品 序列号 价格 我正在寻找一种方法来显示一个新的行,该行将显示库存产品的总价格 有人能给我指一下正确的方向吗 多谢各位 我已经对此进行了研究,但当我显示自定义视图时,我不能像在“标准”模型视图上那样执行CRUD操作: 附言:我正在学习python,所以如果这是一个我浪费您时间的基本问题,请原谅。参考答案中概述的方法也可以用于Flask Admin Dashboard 我已经在Github上创建了一个示例项目 以下是基本
附言:我正在学习python,所以如果这是一个我浪费您时间的基本问题,请原谅。参考答案中概述的方法也可以用于Flask Admin Dashboard 我已经在Github上创建了一个示例项目 以下是基本概念。 要显示摘要表,视图需要:
- 将摘要值插入到视图中
- 定义要使用的Jinja模板,并对其进行适当修改以使用注入的摘要值
templates/admin/model/summary_list.html
是Flask admin Bootstrap 3模板文件夹的直接副本
请注意文件名,summary\u list.html
,因为这在视图定义的render
方法中使用
在第163行插入了以下html块:
{# This adds the summary data #}
{% for row in summary_data %}
<tr>
{% if actions %}
<td>
{# leave this empty #}
</td>
{% endif %}
{# This is the summary line title and goes in the action column, note that the action may not be visible!!! #}
{% if admin_view.column_display_actions %}
<td><strong>{{ row['title'] or ''}}</strong></td>
{% endif %}
{# This is the summary line data and goes in the individual columns #}
{% for c, name in list_columns %}
<td class="col-{{c}}">
<strong>{{ row[c] or ''}}</strong>
</td>
{% endfor %}
</tr>
{% endfor %}
第75行,覆盖视图的渲染(self、template、**kwargs)
方法:
def render(self, template, **kwargs):
# we are only interested in the summary_list page
if template == 'admin/model/summary_list.html':
# append a summary_data dictionary into kwargs
# The title attribute value appears in the actions column
# all other attributes correspond to their respective Flask-Admin 'column_list' definition
_current_page = kwargs['page']
kwargs['summary_data'] = [
{'title': 'Page Total', 'name': None, 'cost': self.page_cost(_current_page)},
{'title': 'Grand Total', 'name': None, 'cost': self.total_cost()},
]
return super(ProjectView, self).render(template, **kwargs)
请注意第66行和第71行提供实际汇总数据的辅助方法,这些方法需要根据需要进行调整:
def page_cost(self, current_page):
# this should take into account any filters/search inplace
_query = self.session.query(Project).limit(self.page_size).offset(current_page * self.page_size)
return sum([p.cost for p in _query])
def total_cost(self):
# this should take into account any filters/search inplace
return self.session.query(func.sum(Project.cost)).scalar()
参考答案中概述的方法也可用于Flask Admin Dashboard 我已经在Github上创建了一个示例项目 以下是基本概念。 要显示摘要表,视图需要:
- 将摘要值插入到视图中
- 定义要使用的Jinja模板,并对其进行适当修改以使用注入的摘要值
templates/admin/model/summary_list.html
是Flask admin Bootstrap 3模板文件夹的直接副本
请注意文件名,summary\u list.html
,因为这在视图定义的render
方法中使用
在第163行插入了以下html块:
{# This adds the summary data #}
{% for row in summary_data %}
<tr>
{% if actions %}
<td>
{# leave this empty #}
</td>
{% endif %}
{# This is the summary line title and goes in the action column, note that the action may not be visible!!! #}
{% if admin_view.column_display_actions %}
<td><strong>{{ row['title'] or ''}}</strong></td>
{% endif %}
{# This is the summary line data and goes in the individual columns #}
{% for c, name in list_columns %}
<td class="col-{{c}}">
<strong>{{ row[c] or ''}}</strong>
</td>
{% endfor %}
</tr>
{% endfor %}
第75行,覆盖视图的渲染(self、template、**kwargs)
方法:
def render(self, template, **kwargs):
# we are only interested in the summary_list page
if template == 'admin/model/summary_list.html':
# append a summary_data dictionary into kwargs
# The title attribute value appears in the actions column
# all other attributes correspond to their respective Flask-Admin 'column_list' definition
_current_page = kwargs['page']
kwargs['summary_data'] = [
{'title': 'Page Total', 'name': None, 'cost': self.page_cost(_current_page)},
{'title': 'Grand Total', 'name': None, 'cost': self.total_cost()},
]
return super(ProjectView, self).render(template, **kwargs)
请注意第66行和第71行提供实际汇总数据的辅助方法,这些方法需要根据需要进行调整:
def page_cost(self, current_page):
# this should take into account any filters/search inplace
_query = self.session.query(Project).limit(self.page_size).offset(current_page * self.page_size)
return sum([p.cost for p in _query])
def total_cost(self):
# this should take into account any filters/search inplace
return self.session.query(func.sum(Project.cost)).scalar()
你的模型是SQLAlchemy模型吗?另请看-这有帮助吗?@pjcunningham这太完美了,非常感谢。您的模型是SQLAlchemy模型吗?另请看-这有帮助吗?@pjcunningham这太完美了,非常感谢。