Why am I getting the error: 'int' object has no attribute 'create'?

Why am I getting the error: 'int' object has no attribute 'create'?

代码:

 @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.0\accountant\models\models.py", line 48, in open_second_class
        AttributeError: 'int' object has no attribute 'create'

使用 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 },
    }