NeoVe NeoVe - 9 months ago 84
Python Question

AttributeError: 'int' object has no attribute 'id' - Odoo v9 community

I've migrated a module which can create

movements, by uploading csv's.

It's functioning quite good, but sometimes, when I upload certain csv's, it throws me this error:


Odoo Server Error

Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/", line 648, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python2.7/dist-packages/openerp/", line 685, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python2.7/dist-packages/openerp/", line 321, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/service/", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/", line 314, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/", line 964, in __call__
return self.method(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/", line 514, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/", line 892, in call_button
action = self._call_kw(model, method, args, {})
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/", line 880, in _call_kw
return getattr(request.registry.get(model), method)(, request.uid, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/", line 421, in old_api
result = new_api(recs, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/", line 425, in new_api
result = [method(rec, *args, **kwargs) for rec in self]
File "/usr/lib/python2.7/dist-packages/openerp/custom_addons/stock_inventory_import/wizard/", line 85, in action_import
val['location_id'] =
AttributeError: 'int' object has no attribute 'id'

The code is this:
def action_import(self):
"""Load Inventory data from the CSV file."""
ctx = self._context
stloc_obj = self.env['stock.location']
inventory_obj = self.env['stock.inventory']
inv_imporline_obj = self.env['stock.inventory.import.line']
product_obj = self.env['product.product']
if 'active_id' in ctx:
inventory = inventory_obj.browse(ctx['active_id'])
if not
raise exceptions.Warning(_("You need to select a file!"))
# Decode the file data
data = base64.b64decode(
file_input = cStringIO.StringIO(data)
location = self.location
reader_info = []
if self.delimeter:
delimeter = str(self.delimeter)
delimeter = ','
reader = csv.reader(file_input, delimiter=delimeter,
except Exception:
raise exceptions.Warning(_("Not a valid file!"))
keys = reader_info[0]
# check if keys exist
if not isinstance(keys, list) or ('code' not in keys or
'quantity' not in keys):
raise exceptions.Warning(
_("Not 'code' or 'quantity' keys found"))
del reader_info[0]
values = {}
actual_date =
inv_name = + ' - ' + actual_date
inventory.write({'name': inv_name,
'imported': True, 'state': 'confirm'})
for i in range(len(reader_info)):
val = {}
field = reader_info[i]
values = dict(zip(keys, field))
prod_location =
if 'location' in values and values['location']:
locat_lst =[('name', '=',
if locat_lst:
prod_location = locat_lst[0]
prod_lst =[('default_code', '=',
if prod_lst:
val['product'] = prod_lst[0].id
if 'lot' in values and values['lot']:
val['lot'] = values['lot']
val['code'] = values['code']
val['quantity'] = values['quantity']
val['location_id'] =
val['inventory_id'] =
val['fail'] = True
val['fail_reason'] = _('No processed')

And the csv's look like this:

id product_id reference code combinacion avanzadastock location quantity qty
2780 Piloto trasero Recambio Ecológico Original M0002780 gsx 600 f 600 1988-1991/4316/A8I 1

This error appears from time to time, often it just works without problems, but some other times throws this error.

It is exactly on

I have csv's with more than 5k items, so it's difficult for me to track the error.

Is this csv related? Or it is a matter of code?

Answer Source

Issue is your logic

prod_location =

Then the following if statement is never entered, and you move to

val['location_id'] =

And the error is thrown