AttributeError: 'NoneType' object has no attribute 'id' - Odoo v8

AttributeError: 'NoneType' object has no attribute 'id' - Odoo v8

此方法将在 state 上写入表单 namedraft 更改为 confirm:

production_type = fields.Selection([
    ('budgeted','Budgeted'),
    ('nonbudgeted','Non Budgeted'),
    ('direct','Direct Order'),
], string='Type of Order', index=True, copy=False,
help=" ")
state = fields.Selection([
        ('draft','Draft'),
        ('confirm','Confirmed'),
        ('inprogress','In progress'),
        ('print_order_inprogress','Print In Progress'),
        ('finished','Finished'),
        ('cancel','Cancel'),
    ], string='State', index=True, copy=False,
    help=" ")

@api.one
def prod_start_func(self):
    name = '/'
    if self.production_type == 'budgeted':
            name = self.env['ir.sequence'].next_by_code('bsi.production.budgeted') or '/'
    elif self.production_type == 'nonbudgeted':
            name = self.env['ir.sequence'].next_by_code('bsi.production.non_budgeted') or '/'
    elif self.production_type == 'direct':
            name = self.env['ir.sequence'].next_by_code('bsi.production.direct') or '/'

    self.write({
            'state': 'confirm',
            'name' : lambda self, cr, uid, context: self.pool.get('ir.sequence').next_by_code(cr, uid, 'bsi.production.order') or '',
            })

但每次我尝试保存它时,它都会抛给我这个:

Traceback (most recent call last):
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 546, in _handle_exception
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 583, in dispatch
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 319, in _call_function
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\service\model.py", line 118, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 316, in checked_call
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 812, in __call__
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 412, in response_wrap
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\web\controllers\main.py", line 944, in call_kw
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\web\controllers\main.py", line 936, in _call_kw
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 268, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 373, in old_api
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 291, in <lambda>
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\models.py", line 4057, in <lambda>
AttributeError: 'NoneType' object has no attribute 'id'

我认为这与此行有关 'name' : lambda self, cr, uid, context: self.pool.get('ir.sequence').next_by_code(cr, uid, 'bsi.production.order') or '', 但如果我将其更改为 'name': name, 我得到相同的结果。

有什么想法吗?

您正在尝试在新 api

中使用旧 api 格式

试试这个: 'name': lambda self: self.env['ir.sequence'].next_by_code('bsi.production.order') 或 '/'

还要确保自身包含单个记录(因为您使用的是@api.one)

如果它包含多条记录使用@api.multi

并且没有可用的记录集使用@api.model

我想我知道这个问题是因为我的上一个回答,你从 create 方法中删除了 return 语句,这是错误的你应该保留它我认为你知道这一点。

      @model
      create(self, vals):
              ....
               ....
               return super(bsi_production_order, self).create(vals)

因为当 create 是 don odoo 期望方法 return 一个对象并且在你的情况下你删除了 return 语句所以调用将 return None 通过默认。当 odoo 尝试访问 returned 记录 (record = None) 的 id 时,他会引发此错误。

记住这条规则总是在你覆盖创建或写入时调用超级,因为创建的真正工作是在 models.py 中。并且 odoo 期望 write 方法为 return True。

另一个例子,如果你需要在创建方法后做逻辑

      @model
       create(self, vals):
          ....
          ....
          record = super(bsi_production_order, self).create(vals)
          ......
          ......
           # always return the object that is created
           return record

在 write 方法中 return 在这种情况下是正确的