首页 > 解决方案 > 使用python动态创建和更新odoo qweb报告中的表行和列

问题描述

在此处输入图像描述

我将上面的这张表作为我的 qweb 报告的最终输出。该表应该是动态生成的。

您需要什么样的数据结构来表示这些数据,以便当前为零的表格单元格的值等于拆分变量后的值。

我想要这样,如果你有一个变量m-x = 20,表格单元格在col m,row x将等于20等等。如果没有这样的组合,则表格单元格等于 0。

请让我知道这是否有意义以及可能的任何澄清。谢谢!

这就是我现在正在做的事情。不确定我的方向是否正确。正在从制造模型打印报告。尽管产品在库存中移动,但我会搜索其来源或参考与制造订单名称匹配的所有移动。

然后我取 lot_id 的名称,加入字典中的键并检查它们是否匹配,然后更新值。

size = {
            'u/s': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0}
            'w': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0}
            'bhl': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0}
        }
for output in self.env['stock.move.line'].search(['|', ('origin', '=', docs.name), ('reference', '=', docs.name)]):
    lot_id = output.lot_id.name
        for i in size:
            for j in size[i]:
                if (j+'-'+i) == lot_id:
                   i[j] = output.qty_done

标签: pythonodooodoo-12odoo-13

解决方案


我通过使用字典来完成这项工作。

Python代码:

size = {
            'u/s': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
            'w': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
            'bhl': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
            'bhp': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
            'rhl': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
            'rhp': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
            'yhl': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
            'yhp': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
            'stains': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
            
        }
        
for output in self.env['stock.move.line'].search(['|', ('origin', '=', docs.name), ('reference', '=', docs.name)]):
    lot_id = output.lot_id.name
    if lot_id:
       for i in size:
           for j in size[i]:
               if (f"{j}-{i}") == lot_id.lower():
                   size[i][j] = output.qty_done

xml代码:

<thead>
    <tr>
      <th>SIZE</th>
      <th>MP/SUP</th>
      <th>TR</th>
      <th>D2</th>
      <th>V</th>
      <th>V1</th>
      <th>D/REJ</th>
      <th>TOTAL</th>
      <th>PERCENTAGE (%)</th>
    </tr>
  </thead>
  <tbody>
     <t t-foreach="size" t-as="i">
         <tr>
            <th>
               <t t-esc="i.upper()" />
            </th>
         <t t-foreach="size[i]" t-as="j">
            <td>
               <t t-esc="size[i][j]" />
            </td>
         </t>
       </tr>
     </t>
 </tbody>

附言。Pandas 似乎是一种快速完成工作的方法。


推荐阅读