Odoo Help


Using Cursor in Mako Report

on 2/26/13, 10:19 AM 2,753 views

How can I get access to a Cursor object from within a mako report? I know I can use the pooler to get a new cursor object (Like in the code below) but as far as I know you have to hard code the database name which is not ideal...

db, pool = pooler.get_db_and_pool(db_name)
cr = db.cursor()

(Note for people reading this: make sure you commit() the transaction and close() the cursor after using it! Like in this example)

Yup: AttributeError: "Field 'cr' does not exist in object 'browse_record(sale.order, 185)'"

on 2/26/13, 10:40 AM

Guewen Baconnier - Software Developer, Camptocamp

--Guewen Baconnier - Software Developer, Camptocamp--

| 6 7 8
Lausanne, Switzerland
--Guewen Baconnier - Software Developer, Camptocamp--

Guewen works at Camptocamp as an Odoo developer since 2010. He is the lead developer of the Odoo Connector framework and the Magento Connector. He is a delegate member of the OCA, a committer and an active contributor to the OCA projects.


Guewen Baconnier - Software Developer, Camptocamp
On 2/26/13, 11:22 AM

When you create a report class, you can modify the scope you will be able to access from the report, by modifying self.localcontext.

You can, as below, add a binding to cr:

class my_report(report_sxw.rml_parse):

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

    def _get_company_vat(self):
        res_users_obj = pooler.get_pool(self.cr.dbname).get('res.users')
        company_vat = res_users_obj.browse

In your report, you'll be able to use time, cr or company_vat().

You'll see that you can also add a binding for methods, this is maybe a better solution than directly access cr from your report.

On a side note, when you have some computation to do in a report, you often better have to compute it before the rendering of the template than during the rendering.

If you do not use a custom class and cannot create it (SaaS for example), you should be able to access to the current cursor with self._cr, if self is a browse_record.

Thanks for the answer - it contains a lot of interesting information, however it is not applicable to my question because I am using Mako reports, and its on SaaS ... is there no other way?

on 2/26/13, 11:25 AM

Edited my response with a way to obtain the cursor using the _cr attribute of browse_record.

Camptocamp SA, Guewen Baconnier - Software Developer, Camptocamp
on 2/26/13, 11:42 AM

The _cr solution worked, thanks very much :)

on 2/26/13, 11:44 AM
alfonso olavarria
On 5/15/13, 1:25 PM

as making a sum total of some fields of my object with mako?

Hi, I have a smatoria of values ​​that I have. % for order in objects: $ {amount_total}-here are the values ​​I want to add at every turn to give me a total

but so I can not because in the mako that I have when I embed this.

<% variable_acumulada = 0 %>

% for order in objects: variable_acumulada + = orden.amount_total % endfor

I print to pdf variable_acumuladora + = orden.amount_total

and if I put it like that.

<% variable_acumulada = 0 %> % for order in objects: variable_acumulada + = $ {orden.amount_total} % endfor

prints for each turn me this.

lap 1 variable_acumuladora + = 86.5

turn 2 variable_acumuladora + = 1.05

Thanks for responding.

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

0 follower(s)


Asked: 2/26/13, 10:19 AM
Seen: 2753 times
Last updated: 3/16/15, 8:10 AM