NeoVe
NeoVe

Reputation: 3897

ProgrammingError: can't adapt type 'stock.location' - Odoo v9

I'm migrating an Odoo v8 module, which is used to upload .csv's into stock.inventory model.

I've fixed a few things, but I still have some bugs on it, like this method:

@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
        val['inventory_id'] = inventory.id
        val['fail'] = True
        val['fail_reason'] = _('No processed')
        inv_imporline_obj.create(val)

Everytime, I click on the button which calls for this function, to upload the csv file it throws me this:

2016-11-02 23:18:38,659 16865 ERROR moto_backup openerp.http: Exception during JSON request handling.
Traceback (most recent call last):
File "/home/kristian/odoov9/odoo-9.0/openerp/http.py", line 648, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/kristian/odoov9/odoo-9.0/openerp/http.py", line 685, in dispatch
result = self._call_function(**self.params)
File "/home/kristian/odoov9/odoo-9.0/openerp/http.py", line 321, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/kristian/odoov9/odoo-9.0/openerp/service/model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "/home/kristian/odoov9/odoo-9.0/openerp/http.py", line 314, in checked_call
result = self.endpoint(*a, **kw)
File "/home/kristian/odoov9/odoo-9.0/openerp/http.py", line 964, in call
return self.method(*args, **kw)
File "/home/kristian/odoov9/odoo-9.0/openerp/http.py", line 514, in response_wrap
response = f(*args, **kw)
File "/home/kristian/odoov9/odoo-9.0/addons/web/controllers/main.py", line 892, in call_button
action = self._call_kw(model, method, args, {})
File "/home/kristian/odoov9/odoo-9.0/addons/web/controllers/main.py", line 880, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/home/kristian/odoov9/odoo-9.0/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/home/kristian/odoov9/odoo-9.0/openerp/api.py", line 421, in old_api
result = new_api(recs, *args, **kwargs)
File "/home/kristian/odoov9/odoo-9.0/openerp/api.py", line 425, in new_api
result = [method(rec, *args, **kwargs) for rec in self]
File "/home/kristian/odoov9/motostion_addons/odoomrp-wip-9.0/stock_inventory_import/wizard/import_inventory.py", line 89, in   action_import
inv_imporline_obj.create(val)
File "/home/kristian/odoov9/odoo-9.0/openerp/api.py", line 248, in wrapper
return new_api(self, *args, **kwargs)
File "/home/kristian/odoov9/odoo-9.0/openerp/models.py", line 4157, in create
record = self.browse(self._create(old_vals))
File "/home/kristian/odoov9/odoo-9.0/openerp/api.py", line 248, in wrapper
return new_api(self, *args, **kwargs)
File "/home/kristian/odoov9/odoo-9.0/openerp/api.py", line 490, in new_api
result = method(self._model, cr, uid, *args, **old_kwargs)
File "/home/kristian/odoov9/odoo-9.0/openerp/models.py", line 4301, in _create
tuple([u[2] for u in updates if len(u) > 2])
File "/home/kristian/odoov9/odoo-9.0/openerp/sql_db.py", line 141, in wrapper
return f(self, *args, **kwargs)
File "/home/kristian/odoov9/odoo-9.0/openerp/sql_db.py", line 220, in execute
res = self._obj.execute(query, params)
ProgrammingError: can't adapt type 'stock.location'

I guess the is something with the new api that I'm missing,

Please, any ideas about this?

Upvotes: 3

Views: 8701

Answers (1)

danidee
danidee

Reputation: 9624

A little mistake, you're trying to pass the stock.location object you got directly from your search query instead of the id

Change this line

if locat_lst:
    prod_location = locat_lst[0]

to

if locat_lst:
    prod_location = locat_lst[0].id

Upvotes: 7

Related Questions