Odoo Help


Can't inherit account.invoice

on 1/25/16, 3:51 PM 1,694 views

Hi everyone.

In odoo 8 I'm trying to inherit the account.invoice module (extension type of inheritance) in order to add new button on sales invoice form to print my customized invoice. However the problem now is, that I'm getting the error

AttributeError: 'int' object has no attribute 'id' when trying to upgrade my module.

My module definition looks like this:

# -*- coding: utf-8 -*-
from openerp import models, fields, api
class Inv(models.Model):
     _inherit = 'account.invoice'
     _name = 'my.inv'
     name = fields.Char(string="Title", required=True)
     description = fields.Text()
     #comment = fields.Text()
     def print_invoice_my(self):
        assert len(self) == 1, 'This option should only be used for a single id at a time.'
        #self.sent = True
        return self.env['report'].get_action(self, 'my.report_invoice_my')

 In __openerp.py__ checked I have:

'depends': ['base','account'],

Can you please suggest what am I doing wrong, thanks regards.

Have you put your button in the inherit view of account.invoice ?

Jignesh Mehta
on 1/26/16, 1:20 AM

Yes I did, just like in this post https://www.odoo.com/forum/help-1/question/how-to-connect-button-with-function-96285

on 1/26/16, 5:40 AM



| 6 5 7
Tbilisi, Georgia
On 1/26/16, 4:05 AM

you should use same "_name" as one of parent class:

# -*- coding: utf-8 -*-
from openerp import models, fields, api, _ from openerp.exceptions import Warning
class Inv(models.Model): _inherit = 'account.invoice' _name = 'account.invoice'
@api.multi def print_invoice_my(self): raise Warning( _("print_invoice_my function gets called on button click!") )

or you can simply remove _name line, because it'll by default stay the same as in parent.

Thanks Temur, you are right I should have removed the _name, but still now the server doesn't even reboot anymore. I guess the problem now is with print_invoice_my function declared in my module. Have I wrote the code correctly to run the report? Thanks again.

on 1/26/16, 5:38 AM

I updated code for u in the answer above. note that there is @api.multi NOT @api.one. and it seems to me that all the field definitions in your code are unnecessary... try the code from answer, run this minimal code first, to make sure you've built your module correctly. then adapt it further to your requirements. If all is ok, then it should give you popup warning (with above text) as you click on your button...

on 1/26/16, 6:04 AM

Thanks Temur. I checked the log file of odoo server, and saw it couldn't start up because I had a demo data defined in my __openerp.py__, while I removed these two fields (name, description) from my class. I adjusted that and now I can see the warning when I click my custom print button. I think I'm almost there. Can you please provide me some sample code to run the report now. My report prints just one record that is one particular invoice, therefore I must have @api.one in my class. How do I run my report properly now? Much thanks again, mate.

on 1/26/16, 11:52 AM

@api.one is not necessary, it's about iterate automatically recordset that you get as a whole in case of @api.multi. you can use @api.multi for one record as well. The reason I recommended you to use @api.multi here instead of @api.one is that I see you've used return statement in your print_invoice_my function code above. When you use @api.one the result of your function (the returned value) is wrapped in a python list, so it's comfortable to use @api.one only in void functions (when you do not return anything from function), otherwise you'll need to take in consideration that the odoo api wraps your return value into the list and in most cases it will not be suitable (however in some cases it may be ok).

on 1/27/16, 8:35 AM

as an example you can take an original python function that gets called when one prints invoice https://github.com/odoo/odoo/blob/dc48744bf7438761e1f25d411ec61755f74c60df/addons/account/account_invoice.py#L376-L383

on 1/27/16, 9:27 AM

Thanks Temur. I already have copied and modified the code from printing the original invoice. However I get the error saying "Bad Report Reference" This report is not loaded into the database: my.report_invoice_my. So I guess my report must be somehow loaded into database. I've posted new thread about this here: https://www.odoo.com/forum/help-1/question/how-to-run-report-from-code-96541 Do you maybe have any suggestion what am I missing. Much thanks again.

on 1/27/16, 12:50 PM

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: 1/25/16, 3:51 PM
Seen: 1694 times
Last updated: 1/26/16, 11:52 AM