[英]AttributeError: 'int' object has no attribute 'id' - Odoo v9 community
I've migrated a module which can create stock.inventory
movements, by uploading csv's. 我已经通过上传csv迁移了一个可以创建stock.inventory
变动的模块。
It's functioning quite good, but sometimes, when I upload certain csv's, it throws me this error: 它的功能相当不错,但是有时候,当我上传某些csv时,它会引发以下错误:
Odoo
Odoo Server Error
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 648, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 685, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 321, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 314, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 964, in __call__
return self.method(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 514, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 892, in call_button
action = self._call_kw(model, method, args, {})
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 880, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 421, in old_api
result = new_api(recs, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", 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/import_inventory.py", line 85, in action_import
val['location_id'] = prod_location.id
AttributeError: 'int' object has no attribute 'id'
The code is this: 代码是这样的:
@api.one
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 self.data:
raise exceptions.Warning(_("You need to select a file!"))
# Decode the file data
data = base64.b64decode(self.data)
file_input = cStringIO.StringIO(data)
file_input.seek(0)
location = self.location
reader_info = []
if self.delimeter:
delimeter = str(self.delimeter)
else:
delimeter = ','
reader = csv.reader(file_input, delimiter=delimeter,
lineterminator='\r\n')
try:
reader_info.extend(reader)
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 = fields.Date.today()
inv_name = self.name + ' - ' + actual_date
inventory.write({'name': inv_name,
'date': fields.Datetime.now(),
'imported': True, 'state': 'confirm'})
for i in range(len(reader_info)):
val = {}
field = reader_info[i]
values = dict(zip(keys, field))
prod_location = location.id
if 'location' in values and values['location']:
locat_lst = stloc_obj.search([('name', '=',
values['location'])])
if locat_lst:
prod_location = locat_lst[0]
prod_lst = product_obj.search([('default_code', '=',
values['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'] = prod_location.id
val['inventory_id'] = inventory.id
val['fail'] = True
val['fail_reason'] = _('No processed')
inv_imporline_obj.create(val)
And the csv's look like this: CSV看起来像这样:
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 location
column. 它正好在location
列上。
I have csv's with more than 5k items, so it's difficult for me to track the error. 我的csv包含超过5k个项目,因此我很难跟踪错误。
Is this csv related? 这与CSV相关吗? Or it is a matter of code? 还是代码问题?
Issue is your logic 问题是你的逻辑
prod_location = location.id
Then the following if statement is never entered, and you move to 然后,从不输入以下if语句,然后转到
val['location_id'] = prod_location.id
And the error is thrown 并引发错误
Yes correct. 是,对的。 Some location don't exist on the system. 系统上不存在某些位置。 That's it throws error. 就是这样抛出错误。 To avoid such error, you can use following trick. 为避免此类错误,您可以使用以下技巧。
prod_location = self.location and self.location.id or False
Means if system has location then prod_location variable set value with id of location otherwise False 意味着如果系统具有位置,则prod_location变量将设置为具有位置ID的值,否则为False
NOTE: 注意:
In model declaration, location_id field set with required=False otherwise you can not create record with location_id=False It will give you integrity error. 在模型声明中, location_id字段设置为required = False,否则您将无法创建location_id = False的记录,这将给您带来完整性错误。
Actually the problem was the csv content 其实问题是csv内容
Because some locations don't exist on the system, then it throws out that error. 由于系统上不存在某些位置,因此它会抛出该错误。
So, need to check for locations that do exist before continuing with the process. 因此,需要在继续该过程之前检查确实存在的位置。
Thank You. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.