gpt4 book ai didi

python - 属性错误: 'NoneType' object has no attribute 'id' - Odoo v8

转载 作者:行者123 更新时间:2023-12-01 02:26:12 26 4
gpt4 key购买 nike

此方法将在 statedraft 更改为 confirm 时写入表单 name:

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 .生产.order') 或 '', 但如果我将其更改为 'name': name, 我得到了相同的结果。

有什么想法吗?

最佳答案

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

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

因为当 create 时,odoo 期望该方法返回一个对象,而在您的情况下,您删除了 return 语句,因此默认情况下调用将返回 None 。当 odoo 尝试访问返回记录的 id (record = None) 时,他会引发此错误。

请记住,当您覆盖 create 或 write 时,此规则始终调用 super,因为创建的真正工作是在 models.py 中。 odoo 期望 write 方法返回 True。

另一个例子,例如如果您需要在创建方法之后执行逻辑

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

在 write 方法中,在这种情况下只需返回 True

关于python - 属性错误: 'NoneType' object has no attribute 'id' - Odoo v8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com