无法在 TransientModel odoo 11 中获取产品
Unable fetch Products in TransientModel odoo 11
到目前为止,我已经阅读了很多问题,但我确信我完全搞混了,因为我无法理解如何在 odoo TransientModel 中使用搜索。
我写了一个代码,从 self
得到 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 ]
但它总是 return
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'
瞬态模型似乎一切正常,问题是您正在尝试读取其键尚不存在的字典值。我的意思是,您在循环中获得的第一个产品的 default_code
是 1,并且您告诉 Python:我想读取字典result
的键1,但是这个是空的,所以报错(需要先填)
您可以在 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'
到目前为止,我已经阅读了很多问题,但我确信我完全搞混了,因为我无法理解如何在 odoo TransientModel 中使用搜索。 我写了一个代码,从 self
得到 Active_idscontext=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 ]
但它总是 return
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'
瞬态模型似乎一切正常,问题是您正在尝试读取其键尚不存在的字典值。我的意思是,您在循环中获得的第一个产品的 default_code
是 1,并且您告诉 Python:我想读取字典result
的键1,但是这个是空的,所以报错(需要先填)
您可以在 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'