Search code examples
pythonpython-2.7odooodoo-8qweb

How to sum calculated in qweb report odoo


How to calculated many record in report qweb use odoo. I tried to create method in report py to calculate

@api.one
@api.depends("total_do")
def _get_total(self):
    batch_ids = self.batch_ids
    total_do = self.total_do
    for in item batch_ids:
        total_do += item.qty_received
        print total_do

and i show in qweb like this :

<div class="col-xs-1" style="text-align:center;border: 1px solid  #568eff;border-left:0px;"> 
    <span t-esc="o.total_do" />
</div>

When I print report, I want to show total from many row . and in my case just show 0


Solution

  • You must set your total method in localcontext of that model (report_sxw.rml_parse)

    def __init__(self, cr, uid, name, context): 
        super(class_name, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
               'time': time,
               '_get_total': self._get_total,
        })
    

    You just need to update that portion from where you calling that total method.

    <div class="col-xs-1" style="text-align:center;border: 1px solid  #568eff;border-left:0px;"> 
        <span t-esc="o._get_total" />
    </div>
    

    And your total method would be like,

    def _get_total(self):
        batch_ids = self.batch_ids
        total_do = 0
        for in item batch_ids:
            total_do += item.qty_received
        return total_do
    

    Refer this for more help