简体   繁体   中英

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

This method will write the form name on state change from draft to 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 '',
            })

But everytime I try to save it, it throws me this:

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'

I think it has something to do with this line 'name' : lambda self, cr, uid, context: self.pool.get('ir.sequence').next_by_code(cr, uid, 'bsi.production.order') or '', but if I change it to 'name': name, I got the same result.

Any ideas?

You are trying to use old api format in new api

try this: 'name': lambda self: self.env['ir.sequence'].next_by_code('bsi.production.order') or '/'

Also make sure self contains single record(as you are using @api.one)

if it contains multiple records use @api.multi

And no record set is available use @api.model

I think i know the problem it's because my last answer, you removed the return statement from create method and this is wrong you should keep it i thought that you knew that.

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

Because when create is don odoo expect the method to return an object and in your case you removed the return statement so the call will return None by default. when odoo tryies to access the id of the returned record (record = None) he raises this error.

Remember this rule always call the super when you override create or write because the real work of creating is in models.py. And odoo expect the write method to return True.

Another example for example if you need to do logic after creting method

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

In write method just return True in cases like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM