【问题标题】:Add multiple objects in OpenERP report在 OpenERP 报表中添加多个对象
【发布时间】:2012-11-18 21:49:24
【问题描述】:

如何将多个对象传递给报告引擎?

我正在尝试创建自定义发票报告,我需要在其中附加来自其他应用程序的数据以显示在发票上。我可以使用 Web 服务将数据输入 OpenERP 服务器,但如何将其传递给报告引擎? 也许是

set_context or (self.localcontext.update())

方法在这里很有用,因为它允许我将自定义变量传递给报告,但如何传递整个对象。

我从其他应用程序导入的内容本质上是一个巨大的表,其中可能包含 100 条与当前合作伙伴相关的记录,我不需要将其保存在 OpenERP 数据库中,只需在生成发票时显示即可。

编辑:

测试我在解析器中的对象

class test_invoice(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
    super(test_invoice, self).__init__(cr, uid, name, context=context)
    self.localcontext.update({
        'time': time,
        'test_var': 'Worked!',
        'get_list': self._get_list,
    })


def _get_list(self):
    result = []
    ress = {'first': '1',
        'second': '2',
        }
    result.append(ress)
    return result

在 rml 文件中

...start of rml file
     <section>
        <para>
            [[ repeatIn(get_list(), 'g')]]
            [[ g.first ]]
        </para>
    </section>

  </story>
</document>

但这会引发错误“强制转换为 Unicode:需要字符串或缓冲区,找到元组”。我们如何在 rml 中显示自定义列表?

谢谢。

【问题讨论】:

    标签: python report openerp erp rml


    【解决方案1】:

    你可以传递整个对象,只要 OpenERP(嗯,你运行 OpenERP 的 python)知道那个对象。否则,您必须以某种方式“代理”该对象。使用 SQLAlchemy 从外部数据库获取数据可能是少数解决方案。你可以处理这样的事情:

    [...somewhere into your parser...]
    self.localcontext.update({'external_rows': session.query(MyObject).filter_by(foo='baz')})
    

    或者如果您正在管理 CSV 数据:

    self.localcontext.updarte({'external_rows': self._get_myrows_from_csv()})
    

    其中_get_myrows_from_csv 例如返回一个字典列表。

    【讨论】:

    • 感谢您的回复,我可以在 rml 中获取单个变量,但列表仍然存在问题,您介意查看 OP 中的编辑吗?
    【解决方案2】:

    除了使用 RML 文件中的结果(字典)之外,您所做的一切都是正确的。试试

    [[ g['first'] ]]
    

    而不是

    [[ g.first ]]
    

    问题是您试图将字典值作为对象属性访问。

    【讨论】:

      【解决方案3】:

      在 RML 文件中:

      <story>
          <section>
              <para>[[ repeatIn(products(), 'p') ]]</para>
              <blockTable colWidths="100.0,100.0" >
                  <tr>
                      <td>
                          <para>[[p.name]]</para>
                      </td>
      
                      <td>
                          <para>[[p.list_price]]</para>
                      </td>
                  </tr>
              </blockTable >
          </section>
      </story>
      

      在报告解析器中:

      class myreport(report_sxw.rml_parse):
          def __init__(self, cr, uid, name, context):
              super(myreport, self).__init__(cr, uid, name, context=context)
      
              self.cr = cr
              self.uid = uid
              self.context = context
      
              self.localcontext.update({
                  'products': self._get_products
                  })
      
          def _get_products(self, ids=[85, 81, 89]):
              return [p for p in self.pool.get('product.product').browse(self.cr, self.uid, ids, self.context)]
      

      希望对你有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多