Skip to Content
Menu
This question has been flagged
2 Replies
6110 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()
     @api.one
     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.


Avatar
Discard

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

Author

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

Best Answer

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.


Avatar
Discard
Author

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.

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...

Author

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.

@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).

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

Author

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.

Related Posts Replies Views Activity
3
Mar 19
2797
1
Mar 15
4098
1
Mar 15
4578
1
Mar 15
3027
1
Aug 24
274