“基于自定义SQL的报告”的版本间的差异
来自Odoo大V社-odoo中文开发手册
2355481564(讨论 | 贡献) (创建页面,内容为“我们所建的报告是建立在常规记录的基础上的。但在某些情况下,在呈现报表时,我们需要以不易处理的方式转换或聚合动态数...”) |
2355481564(讨论 | 贡献) |
||
第21行: | 第21行: | ||
user_id = fields.Many2one('res.users', 'Responsible') | user_id = fields.Many2one('res.users', 'Responsible') | ||
date_deadline = fields.Date('Deadline') </nowiki> | date_deadline = fields.Date('Deadline') </nowiki> | ||
+ | |||
+ | 要加载这个文件,我们需要在__init__.py文件顶部添加from . import reports,并且在reports/__init__.py文件中添加from . import todo_task_report。 | ||
+ | |||
+ | sql 属性会自动翻译为SQL语句,创建时重写覆盖数据库表。我们希望它创建一个数据库视图来提供报告所需的数据。虽然我们的SQL查询非常简单,但我们可以为视图使用任何有效的SQL查询。 | ||
+ | |||
+ | 我们还映射了我们需要的ORM字段类型的字段,所以它们在此模型上生成的记录集上可用。 | ||
+ | |||
+ | 接下来,我们可以为这个模型添加一个新的报告: | ||
+ | <nowiki> | ||
+ | reports/todo_model_report.xml: | ||
+ | <odoo> | ||
+ | <report id="action_todo_model_report" | ||
+ | string="To-do Special Report" | ||
+ | model="todo.task" | ||
+ | report_type="qweb-html" | ||
+ | name="todo_report.report_todo_task_special" | ||
+ | /> | ||
+ | <template id="report_todo_task_special"> | ||
+ | <t t-call="report.html_container"> | ||
+ | <t t-call="report.external_layout"> | ||
+ | <div class="page"> | ||
+ | <!-- Report page content --> | ||
+ | <table class="table table-striped"> | ||
+ | <tr> | ||
+ | <th>Title</th> | ||
+ | <th>Owner</th> | ||
+ | <th>Deadline</th> | ||
+ | </tr> | ||
+ | <t t-foreach="docs" t-as="o"> | ||
+ | <tr> | ||
+ | <td class="col-xs-6"> | ||
+ | <span t-field="o.name" /> | ||
+ | </td> | ||
+ | <td class="col-xs-3"> | ||
+ | <span t-field="o.user_id" /> | ||
+ | </td> | ||
+ | <td class="col-xs-3"> | ||
+ | <span t-field="o.date_deadline" /> | ||
+ | </td> | ||
+ | </tr> | ||
+ | </t> | ||
+ | </table> | ||
+ | </div> | ||
+ | </t> | ||
+ | </t> | ||
+ | </template> | ||
+ | </odoo></nowiki> | ||
+ | |||
+ | 对于更复杂的情况,我们可以使用不同的解决方案:向导。为此,我们应该创建一个生成相应行的瞬态模型,包括其中的标题、用户引入的报表参数,报告所使用的数据。这些行是由一个包含我们任何逻辑的模型方法生成的。强烈建议从现有的类似报告中获得灵感。 |
2018年1月23日 (二) 09:32的最新版本
我们所建的报告是建立在常规记录的基础上的。但在某些情况下,在呈现报表时,我们需要以不易处理的方式转换或聚合动态数据.
一种方法是编写一个SQL查询来构建我们需要的数据集,通过一个特殊的模型公开这些结果,并使我们的报告在这个记录集上工作。
因此,我们需要创建一个包含以下代码的文件reports/todo_task_report.py:
# -*- coding: utf-8 -*- from odoo import models, fields class TodoReport(models.Model): _name = 'todo.task.report' _description = 'To-do Report' _sql = """ CREATE OR REPLACE VIEW todo_task_report AS SELECT * FROM todo_task WHERE active = True """ name = fields.Char('Description') is_done = fields.Boolean('Done?') active = fields.Boolean('Active?') user_id = fields.Many2one('res.users', 'Responsible') date_deadline = fields.Date('Deadline')
要加载这个文件,我们需要在__init__.py文件顶部添加from . import reports,并且在reports/__init__.py文件中添加from . import todo_task_report。
sql 属性会自动翻译为SQL语句,创建时重写覆盖数据库表。我们希望它创建一个数据库视图来提供报告所需的数据。虽然我们的SQL查询非常简单,但我们可以为视图使用任何有效的SQL查询。
我们还映射了我们需要的ORM字段类型的字段,所以它们在此模型上生成的记录集上可用。
接下来,我们可以为这个模型添加一个新的报告:
reports/todo_model_report.xml: <odoo> <report id="action_todo_model_report" string="To-do Special Report" model="todo.task" report_type="qweb-html" name="todo_report.report_todo_task_special" /> <template id="report_todo_task_special"> <t t-call="report.html_container"> <t t-call="report.external_layout"> <div class="page"> <!-- Report page content --> <table class="table table-striped"> <tr> <th>Title</th> <th>Owner</th> <th>Deadline</th> </tr> <t t-foreach="docs" t-as="o"> <tr> <td class="col-xs-6"> <span t-field="o.name" /> </td> <td class="col-xs-3"> <span t-field="o.user_id" /> </td> <td class="col-xs-3"> <span t-field="o.date_deadline" /> </td> </tr> </t> </table> </div> </t> </t> </template> </odoo>
对于更复杂的情况,我们可以使用不同的解决方案:向导。为此,我们应该创建一个生成相应行的瞬态模型,包括其中的标题、用户引入的报表参数,报告所使用的数据。这些行是由一个包含我们任何逻辑的模型方法生成的。强烈建议从现有的类似报告中获得灵感。