Search code examples
pythonpython-3.xpostgresqlodooodoo-11

Unable fetch Products in TransientModel odoo 11


So far i had read many questions but i am sure that i am total a mix up because i am unable understand how to use Search at odoo TransientModel. I had written a code that get Active_ids from self

context=self.env.context.get('active_ids')

I am supposing these actives_ids as product_tmpl_id but when i tried to use them

  product_recs = produtc_obj.search([('product_tmpl_id', 'in', context)])
    print(product_recs)
    result = {}
    for rec in product_recs:
        print(rec.default_code )
        result[rec.default_code ]

but its always return

result[rec.default_code ]
KeyError: '1'

Here is my full code

import logging
from odoo import models, fields, api
from odoo.exceptions import Warning
_logger = logging.getLogger(__name__)

class product_export_to_rakuten(models.TransientModel):
    _name = 'rakuten_ftp.export_product'

    @api.multi
    def export_products(self):
        # check for more than one orders.
       # print(self.env)
        context=self.env.context.get('active_ids')
        produtc_obj = self.env['product.product']

        product_recs = produtc_obj.search([('product_tmpl_id', 'in', context)])
        print(product_recs)
        result = {}
        for rec in product_recs:
            print(rec.default_code )
            result[rec.default_code ]

Here is the Error

 Traceback (most recent call last):
      File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 647, in _handle_exception
        return super(JsonRequest, self)._handle_exception(exception)
      File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 307, in _handle_exception
        raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
      File "C:/Odoo_Source_Codes/odoo11\odoo\tools\pycompat.py", line 87, in reraise
        raise value
      File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 689, in dispatch
        result = self._call_function(**self.params)
      File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 339, in _call_function
        return checked_call(self.db, *args, **kwargs)
      File "C:/Odoo_Source_Codes/odoo11\odoo\service\model.py", line 97, in wrapper
        return f(dbname, *args, **kwargs)
      File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 332, in checked_call
        result = self.endpoint(*a, **kw)
      File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 933, in __call__
        return self.method(*args, **kw)
      File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 512, in response_wrap
        response = f(*args, **kw)
      File "C:\Odoo_Source_Codes\odoo11\addons\web\controllers\main.py", line 934, in call_button
        action = self._call_kw(model, method, args, {})
      File "C:\Odoo_Source_Codes\odoo11\addons\web\controllers\main.py", line 922, in _call_kw
        return call_kw(request.env[model], method, args, kwargs)
      File "C:/Odoo_Source_Codes/odoo11\odoo\api.py", line 689, in call_kw
        return call_kw_multi(method, model, args, kwargs)
      File "C:/Odoo_Source_Codes/odoo11\odoo\api.py", line 680, in call_kw_multi
        result = method(recs, *args, **kwargs)
      File "C:\Odoo_Source_Codes\odoo11\custom_addons\rakuten_ftp\wizard\export_product.py", line 22, in export_products
        result[rec.default_code ]
    KeyError: '1'

Solution

  • Everything seems to be fine with the Transient Model, the problem is that you're trying to read a dictionary value whose key doesn't exist yet. I mean, the default_code of the first product you get in the loop is 1, and you're telling Python: I want to read the value of the key 1 of the dictionary result, but this one is empty, so you get the error (you need to fill it in first).

    You can reply the error in a Python console, this is what is happening to you:

    >>> result = {}
    >>> result['1']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: '1'
    

    You should do something like this to make it work:

    >>> result = {}
    >>> result['1'] = 'Your value'  # result.update({'1': 'Your value', })
    >>> result['1']
    'Your value'