3
votes

I'm new in odoo. I need one custom report but don't understand all about this.

In odoo documentation stay:

from openerp import api, models

class ParticularReport(models.AbstractModel):
    _name = 'report.module.report_name'

    @api.multi
    def render_html(self, data=None):
        report_obj = self.env['report']
        report = report_obj._get_report_from_name('module.report_name')
        docargs = {
            'doc_ids': self._ids,
            'doc_model': report.model,
            'docs': self,
        }
        return report_obj.render('module.report_name', docargs)

I want from res.users in qweb display all user and field (create_date, name, descrption) what do I need to change in above example and how do I create view?

Any similar examples for a beginner?

1
Take a look at my existing answer. It might have the information you need. stackoverflow.com/questions/40290627/…Phillip Stack
@PhillipStack Thanks for replay, In your example I don't see view (with field) and how call render_htmlAnastasia_
When you have properly registered your report Odoo will call that method for you. Are you just trying to call your report using the more drop down? I am expanding an answer for you right now. Give me a moment.Phillip Stack
@PhillipStack What is (_get_report_from_name, your_model1, your_model2) any descripion? Can you describe how create view (report_name) and how add foreach loop?Anastasia_
I will take a look at this later. I am sorry. I have some work to do at the moment.Phillip Stack

1 Answers

4
votes

If you take a look at my previous answer you can see how you define a custom report for your model. The important part is that you find the report object and call the render method on it. The next most important part is to pass the arguments you wish to pass to it.

Once you have done this you can access those values by key name in the qweb report. I understand that there are a lot of concepts and contexts and blah blah blahs to digest for a new Odoo developer. I will try to provide an example. And do my best to keep it as simple as possible. If it makes you feel any better I spent the better part of an afternoon the first time I tried to do this with a custom report.

You will need to define a AbstractModel class for your report. Include this is a .py file which is included in __init__.py. You probably will want some logging just to see what is happening unless you are so lucky to have it work first time.

I was not.

import logging
_logger = logging.getLogger(__name__)

class YourReport(models.AbstractModel):
    _name = 'report.your_addon.report_template_id'

    @api.multi
    def render_html(self, data=None):
        _logger.info("RUNNING REPORT")
        report_obj = self.env['report']
        report = report_obj._get_report_from_name('your_addon.report_template_id')
        docs = self.env['your_addon.your_model'].search([('something','=','something')])   
        docargs = {
            'doc_model': report.model,
            'docs': docs,
        }
        return report_obj.render('your_addon.report_template_id', docargs)

In the class above we override the render_html method. The report model method _get_report_from_name is just a method which returns the report object for a named report. You can replace this with any odoo orm that returns a browse object for a report.

You must then create the xml definition for your report.

<openerp>
  <data>
    <report
        id="report_template_id"
        model="your_addon.model"
        string="Report Title"
        name="your_addon.report_template_view"
        file="your_addon.report_template"
        report_type="qweb-pdf"/>

    <template id="report_template_view">
        <t t-call="report.html_container">
            <t t-foreach="docs" t-as="doc">
                <t>
                    <div class="page">
                        <t t-esc="doc.field_name"/>
                    </div>
                </t>
            </t>
        </t>
    </template>
  </data>
</openerp>

If you create a button in any model's form view that executes a method returning a report action the render_html function for your report will be called.

    @api.multi
    def print_report(self):
    return {
        'type' : 'ir.actions.report',
        'report_name': 'report_template_id'
        'datas': {
            'ids': [ self.id ],
            'model': 'your_addon.your_model'
        }
    }