Odoo Help


How to use python function in my QWeb report?

Gary Heldmann
on 5/16/16, 10:49 AM 662 views

I would like to use in my QWeb report the element "item". What I should write in my report ? "item" is an attribute in tabData. You can see my python code below. I have already conffigure the report parser and it works.

This is my python code :

def getLines (self, objects):

            tabData = []
            print objects
            for i in range (7):
                tabData [i] ['jour']      = self.tabJour [i]
                tabData [i] ['commandes'] = {}

            for o in objects:
                indJour = int(o.jour) - 1

                if indJour not in range(7): continue

                commande = o.order_id.name

                if not (tabData [indJour]['commandes'].has_key (commande)):
                    tabData [indJour]['commandes'][commande] = {}
                    tabData [indJour]['commandes'][commande]['items'] = []
                    tabData [indJour]['commandes'][commande]['partner'] = o.order_partner_id.name

                bois    = o.bois1.code_fini
                bois2   = o.bois2.code_fini
                metal   = o.metal.code_fini
                tissus  = o.tissu.code_fini
                poignee = o.poignee.code_fini
                patte   = o.patte.code_fini
                cfg     = o.config

                optionTissu = o.tissu.couleur_finifr

                qty           = int(o.product_uom_qty)
                qtyEmballe    =  0
                qtyPeinture   = qty - qtyEmballe - int(o.FabPeint)
                qtyTeinture   = qty - qtyEmballe - int(o.FabTeint)
                qtyRembourage = qty - qtyEmballe - int(o.ProdFTissus)
                qtyMetal      = qtyPeinture - int(o.ProdFMetal)
                qtyBois       = qtyTeinture - int(o.ProdFBois)

                if not (qtyMetal) or qtyMetal < 0:
                    qtyMetal = 0
                if not (qtyBois) or qtyBois < 0:
                    qtyBois = 0
                if not (qtyPeinture) or qtyPeinture < 0:
                    qtyPeinture = 0
                if not (qtyTeinture) or qtyTeinture < 0:
                    qtyTeinture = 0
                if not (qtyRembourage) or qtyRembourage < 0:
                    qtyRembourage = 0

                if bois == '.':
                    bois = None
                if bois2 == '.':
                    bois2 = None
                if metal == '.':
                    metal = None
                if tissus == '.':
                    tissus = None
                if poignee == '.':
                    poignee = None
                if patte == '.':
                    patte = None
                if cfg == '.':
                    cfg = None

                if optionTissu == "NONE":

                tabData [indJour]['commandes'][commande]['items'].append ({
                        'item': o.product_id.default_code,
                        'qty': qty,
                        'metal': metal,
                        'bois': bois,
                        'bois2': bois2,
                        'tissus': tissus,
                        'poignee': poignee,
                        'patte': patte,
                        'optionTissu': optionTissu,
                        'cfg': cfg,
                        'notes': o.notes,
                        'qtyEmballe': qtyEmballe,
                        'qtyMetal': qtyMetal,
                        'qtyBois': qtyBois,
                        'qtyPeinture': qtyPeinture,
                        'qtyTeinture': qtyTeinture,
                        'qtyRembourage': qtyRembourage,
            print "FIN"
            return tabData

Thank you for your help.

Niraj Pajwani
On 5/17/16, 6:30 AM

Create .py file to call python function:

  • .py

    # -*- coding: utf-8 -*-

    from openerp import api, models

    class first_report_method(models.AbstractModel):

         _name = 'report.module_name.report_template'


         def render_html(self, data=None):

              report_obj = self.env['report']

             report = report_obj._get_report_from_name('module_name.report_template')

             docargs = {

                 'doc_ids': self._ids,

                  'doc_model': report.model,

                 'docs': self,

                 'getLines': self._getLines,


              return report_obj.render('module_name.report_template', docargs)

         def _getLines(self, object):

             # function body...

             return tabData

    # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

  • Call python function from .xml(report template) using:

    <div t-esc="getLines(o)"/>


Prakash Sharma

--Prakash Sharma--
| 2 1 4
Delhi, India
--Prakash Sharma--

I like to surround myself with brave, creative people.

My motivation for learning different technologies and algos drive me. I love challenges of a steep learning curve which I believe makes me a better learner. I have a strong desire to learn different technologies, which would enable me to become a full stack developer.

Prakash Sharma
On 5/17/16, 1:42 AM
<t t-set="lines" t-value="o.getLines()"></t>

#Place the method in the model of active object .

#set it's values in side the lines variable. 

#Now iterate it's values using for-each 

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 5/16/16, 10:49 AM
Seen: 662 times
Last updated: 5/17/16, 9:03 AM