首页 > 解决方案 > Run self._cr.execute on Compute Field

问题描述

On Odoo 12, Im trying run self._cr.execute on compute but return me Null, I tried domain and is working but because the Query i will use is complex i need to do it with SQL Query.

    _inherit ='purchase.order.line'  

    partner1 = fields.Many2one('res.partner', string='Vendor 1', compute='_compute_vendors', copy = True, store=True, readonly= False)
    partner2 = fields.Many2one('res.partner', string='Vendor 2', compute='_compute_vendors', copy = True, store=True, readonly= False)
    partner3 = fields.Many2one('res.partner', string='Vendor 3', compute='_compute_vendors', copy = True, store=True, readonly= False)

    @api.depends('product_id')
    def _compute_vendors(self):
        vendors = []
        vendors.append(self._cr.execute("""SELECT partner_id FROM purchase_order_line"""))

Any solution?

标签: pythonodooodoo-12

解决方案


执行 return None,检索您可以使用 fetchall 的选定记录:

  # first execute the query
  self._cr.execute("""SELECT partner_id FROM purchase_order_line""")
  # fetc rows
  vendors = [r[0] for r in self._cr.fetchall()]

推荐阅读