在 Odoo 14 中使用来自 purchase.order.line 的搜索功能时出错

Error on using Search function from purchase.order.line in Odoo 14

我有这样的操作:

. . .
<record id="confirm_action" model="ir.actions.server">
<field name="name">Confirm</field>
<field name="binding_model_id" ref="my_module.model_purchase_order_line"/>
<field name="model_id" ref="my_module.model_purchase_order_line"/>
<field name="state">code</field>
<field name="code">
  action = records.confirm_line()
</field>
</record>
. . .

我的模型中有这段代码:

class purchase_order_line_inherit(models.Model):
_inherit = "purchase.order.line"

def confirm_line(self):
    for line in self:
        purchase_orders = self.env['purchase.order.line'].search(['product_id.id','=',line.product_id.id])

这是错误:

Odoo Server Error Traceback (most recent call last): File "/home/odoo/odoo/odoo/addons/base/models/ir_http.py", line 237, in _dispatch result = request.dispatch() File "/home/odoo/odoo/odoo/http.py", line 683, in dispatch result = self._call_function(**self.params) File "/home/odoo/odoo/odoo/http.py", line 359, in _call_function return checked_call(self.db, *args, **kwargs) File "/home/odoo/odoo/odoo/service/model.py", line 94, in wrapper return f(dbname, *args, **kwargs) File "/home/odoo/odoo/odoo/http.py", line 347, in checked_call result = self.endpoint(*a, **kw) File "/home/odoo/odoo/odoo/http.py", line 912, in call return self.method(*args, **kw) File "/home/odoo/odoo/odoo/http.py", line 531, in response_wrap response = f(*args, **kw) File "/home/odoo/odoo/addons/web/controllers/main.py", line 1733, in run result = action.run() File "/home/odoo/odoo/odoo/addons/base/models/ir_actions.py", line 629, in run res = runner(run_self, eval_context=eval_context) File "/home/odoo/odoo/odoo/addons/base/models/ir_actions.py", line 498, in _run_action_code_multi safe_eval(self.code.strip(), eval_context, mode="exec", nocopy=True) # nocopy allows to return 'action' File "/home/odoo/odoo/odoo/tools/safe_eval.py", line 346, in safe_eval raise ValueError('%s: "%s" while evaluating\n%r' % (ustr(type(e)), ustr(e), expr)) Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/home/odoo/odoo/odoo/http.py", line 639, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "/home/odoo/odoo/odoo/http.py", line 315, in _handle_exception raise exception.with_traceback(None) from new_cause ValueError: <class 'TypeError'>: "'int' object is not subscriptable" while evaluating 'action = records.confirm_line()'

我想做的只是获取与我之前选择的行具有相同产品的采购订单行的数据。我做错了什么?

这行 purchase_orders = self.env['purchase.order.line'].search(['product_id.id','=',line.product_id.id]).

给我错误

最后一段代码在某些方面似乎有误。首先,您不会得到订单,而是订单行。所以重命名变量purchase_orders。其次,您不需要域左侧部分中的 .id。第三,域语法错误,因为域必须是特殊逻辑运算符 and/or 三元组的列表。但现在它是一个包含 2 个字符串和一个整数的列表。

所以最终方法应该如下所示:

def confirm_line(self):
    for line in self:
        purchase_order_lines = self.search([('product_id','=',line.product_id.id)])
        # here something has to be done, because right now
        # nothing will happen at all