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

标签 python odoo odoo-8

此方法将在 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/

相关文章:

python - Flask 中的全局变量是线程安全的吗?如何在请求之间共享数据?

python - Python中变量的绑定(bind)顺序

python - 使用 Python 字典通过 Plotly Express 创建条形图

python - 尝试加载特殊字符(非空白控件)时出现 ruamel.yaml ReaderError

unit-testing - ODOO [V8] 单元测试

python - 页脚字符串未显示在 View 中 - Odoo v8

python - 错误 : command 'gcc' failed with exit status when installing psycopg2

python - 使用 python 为 action server 解析电子邮件

odoo - 如何在 Odoo 模板语言中使用 if

python - Odoo 分层 TreeView