3

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?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Anastasia_
  • 289
  • 1
  • 3
  • 17
  • Take a look at my existing answer. It might have the information you need. http://stackoverflow.com/questions/40290627/custom-report-through-python-odoo-9/40291820#40291820 – Phillip Stack Jan 30 '17 at 20:08
  • @PhillipStack Thanks for replay, In your example I don't see view (with field) and how call render_html – Anastasia_ Jan 30 '17 at 20:17
  • 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 Jan 30 '17 at 20:22
  • @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_ Jan 30 '17 at 20:23
  • I will take a look at this later. I am sorry. I have some work to do at the moment. – Phillip Stack Jan 30 '17 at 21:00

1 Answers1

4

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'
        }
    }
Phillip Stack
  • 3,308
  • 1
  • 15
  • 24
  • @Philip Stack Tnx for description, I my example I want dispalay data from res.users is this correct code http://imgur.com/a/k6U0N What is different between your_model in docs and report.model in docargs? – Anastasia_ Jan 30 '17 at 21:23
  • There is no difference. It should be the same model. `report.model` is the `model` attribute of the report browse object. Some of this I adapted from odoo's documentation. In reality you could just use `your_addon.your_model` – Phillip Stack Jan 30 '17 at 21:45
  • I think if your model is Anastasio.res.users then the doc_model should be the same. You should be able to test the validity of your model name by using erppeek or even odoo shell. These are great tools. You can test your methods in the odoo shell interactively which will tell you if you are on the right track or not. – Phillip Stack Jan 30 '17 at 21:50
  • Start your server manually. Like this `./odoo.py shell -d database_name or in version 10 ./odoo-bin shell -d database_name – Phillip Stack Jan 30 '17 at 21:51
  • Then just start browsing records and executing commands like you dont have a care in the world. – Phillip Stack Jan 30 '17 at 21:51
  • Thanks for help but I can't generate report! Here is my source http://imgur.com/a/86Jry – Anastasia_ Jan 31 '17 at 11:29
  • I will take a look when I get a moment today. – Phillip Stack Jan 31 '17 at 14:17
  • In your report definition the model attribute. Remove your model name. As bizarre as that may seem. Just `report.report_template_id`. – Phillip Stack Jan 31 '17 at 17:37
  • @Philip Stack Sorry but I don't understand your last comment, In .xm file I'm change model name from report.test_ana.report_template_id to report.report_template_id and get error report.report_template_id: (, KeyError(u'report.report_template_id',), ) – Anastasia_ Feb 01 '17 at 11:13
  • Also in render_html im update model to report.report_template_id – Anastasia_ Feb 01 '17 at 11:17