在TransientModel odoo 11中无法获取产品

时间:2018-04-05 07:50:29

标签: python python-3.x postgresql odoo odoo-11

到目前为止,我已经阅读了很多问题,但我确信我完全混淆了因为我无法理解如何在odoo TransientModel上使用搜索。 我编写了一个从自我获取 Active_ids 的代码

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

我假设这些actives_ids为 product_tmpl_id但是当我尝试使用它们时

  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 ]

但它总是返回

result[rec.default_code ]
KeyError: '1'

这是我的完整代码

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 ]

这是错误

 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'

1 个答案:

答案 0 :(得分:2)

瞬态模型似乎一切都很好,问题是你正在尝试读取其密钥尚不存在的字典值。我的意思是,你在循环中得到的第一个产品的default_code 1 ,你告诉Python:我想读取密钥 1的值result的em>,但是这个是空的,所以你得到错误(你需要先填写它)。

您可以在Python控制台中回复错误,这就是您发生的事情:

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

你应该做这样的事情来使其发挥作用:

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