为什么我会收到错误:' int'对象没有属性'创建'?

时间:2017-12-09 14:35:01

标签: python python-2.7 openerp odoo-8 odoo-9

代码:

 @api.multi
    def open_second_class(self):
        ac = self.env['ir.model.data'].xmlid_to_res_id('account.invoice_form', raise_if_not_found=True)
        write_obj = ac.create({'partner_id': self.name_id})
        accountant = False
        for o in self:
            accountant = o.id
        result = {
            'name': '2nd class',
            'view_type': 'form',
            'res_model': 'account.invoice',
            'res_id': write_obj.id,
            'view_id': False,
            'context': {'default_id_tbl1': accountant},
            'type': 'ir.actions.act_window',
            'view_mode': 'form',
            'target': 'new',
            'domain': '[]'
        }
        return result

错误:

Traceback (most recent call last):
          File "C:\Program Files\Odoo 9.0-20170919\server\.\openerp\http.py", line 650, in _handle_exception
          File "C:\Program Files\Odoo 9.0-20170919\server\.\openerp\http.py", line 687, in dispatch
          File "C:\Program Files\Odoo 9.0-20170919\server\.\openerp\http.py", line 323, in _call_function
          File "C:\Program Files\Odoo 9.0-20170919\server\.\openerp\service\model.py", line 118, in wrapper
          File "C:\Program Files\Odoo 9.0-20170919\server\.\openerp\http.py", line 316, in checked_call
          File "C:\Program Files\Odoo 9.0-20170919\server\.\openerp\http.py", line 966, in __call__
          File "C:\Program Files\Odoo 9.0-20170919\server\.\openerp\http.py", line 516, in response_wrap
          File "C:\Program Files\Odoo 9.0-20170919\server\openerp\addons\web\controllers\main.py", line 899, in call_button
          File "C:\Program Files\Odoo 9.0-20170919\server\openerp\addons\web\controllers\main.py", line 887, in _call_kw
          File "C:\Program Files\Odoo 9.0-20170919\server\.\openerp\api.py", line 250, in wrapper
          File "C:\Program Files\Odoo 9.0-20170919\server\.\openerp\api.py", line 381, in old_api
          File "C:\Users\Amandnoever\AppData\Local\OpenERP S.A.\Odoo\addons\9.0\accountant\models\models.py", line 48, in open_second_class
        AttributeError: 'int' object has no attribute 'create'

1 个答案:

答案 0 :(得分:0)

使用self.env.ref('external_id')代替xmlid_to_res_id方法。但我认为你想要做的是这样的事情:

@api.multi
def open_second_class(self):
    self.ensure_one()
    ac = self.env.ref('account.invoice_form')
    invoice = self.env['account.invoice'].create({
        'partner_id': self.name_id.id
    })

    return {
        'name': '2nd class',
        'type': 'ir.actions.act_window',
        'res_model': 'account.invoice',
        'res_id': invoice.id,
        'view_type': 'form',
        'view_mode': 'form',
        'target': 'new',
        'context': {'default_id_tbl1': self.env.id },
    }