Search code examples
templatesreportodooodoo-9odoo-view

How to create odoo 9.0 QWeb reports step by step


I spent more than 5 hours on searching in google about creating reports in odoo 9.0 but still nothing, I want to make report which looks like tree view, in pdf, using Qweb, Everything what I found was Invoices, but I don't know how to make report in my example.

Let's assume for example that I have folder in odoo addons 'example' with model(example.py, init.py) and view(example_view.xml) folder and init.py, openerp.py, you know simpliest module, and my question is: Tell me what I must to add and where, what I must to write to XML to make a simple report which looks like tree view(this view is in view folder) and nothing more.

I'm example-learning person and I need example to understand something.

Thanks for answer :)


Solution

  • To create a simple report do the following.

    1. Define Report xml file

      /addons/example/views/example_report.xml

    2. Load the xml file in your addon by referencing it in

      /addons/example/__openerp__.py

    in the data section with other xml files.

    'data': ['views/example_report.xml'],
    
    1. Update your addon.

    If in the list view for you addon you should be able to select a record (check the checkbox) and then in the more drop down run the report. Or in the form view for the model you should also be able to click on more and run the report from there.

    Note: wkhtmltopdf must be properly installed in order for any of this to work. There are instructions at wkhtmltopdf.org (ensure version 0.12.1 at a minimum)

    Here is a simple xml report definition. Lets presume you have a fictional model example.model_name with a name (char), and subrecords (one2many), and the subrecords model has a id,name,and date fields.

    <openerp>
        <data>
            <report
                id="report_example_model_name"
                model="example.model_name"
                string="Example Report"
                name="example.report_example_report_view"
                file="example.report_model_name"
                report_type="qweb-pdf"/>
    
            <template id="report_example_report_view">
                <t t-call="report.html_container">                    
                    <!-- REMEMBER, docs is the selected records either in form view or checked in list view (usually). So the line below says use the following template for each record that has been selected. -->
                    <t t-foreach="docs" t-as="doc">
                        <t>          
                         <div class="page">    
                            <h1>Report For <t t-esc="doc.name"/></h1>
                            <table>
                             <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Date</th>
                             </tr>
    
                             <t t-foreach="doc.subrecord" t-as="o">
                                 <tr>
                                     <td><t t-esc="o.id"/></td>
                                     <td><t t-esc="o.name"/></td>
                                     <td><t t-esc="o.date"/></td>
                                 </tr>
                             </t>
    
                            </table>    
                         </div>
                        </t>
                    </t>
                </t>
            </template>
        </data>
    </openerp>