Python 如何将动态域设置为多个字段
我想根据many2one字段onchange函数更改many2many字段的检索值集,它与另一个many2one字段配合得很好,但似乎无法过滤many2many字段的结果 我的代码如下Python 如何将动态域设置为多个字段,python,odoo,Python,Odoo,我想根据many2one字段onchange函数更改many2many字段的检索值集,它与另一个many2one字段配合得很好,但似乎无法过滤many2many字段的结果 我的代码如下 class CustomPurchase(models.Model): _name = 'custom.purchase' _description = 'Purchase Record' supplier_id = fields.Many2one('custom.supplier',
class CustomPurchase(models.Model):
_name = 'custom.purchase'
_description = 'Purchase Record'
supplier_id = fields.Many2one('custom.supplier', string='Supplier', required=True)
product_ids = fields.Many2many('custom.supply.line', string='Purchase Lines', required=True)
@api.onchange('supplier_id')
def onchange_supplier(self):
selected_lines = []
if self.supplier_id:
for rec in self:
selected_lines = rec.env['custom.supply.line'].search([('supplier_id', '=', rec.supplier_id.id)])
domain = {'product_ids': [('id', '=', selected_lines.id)]}
return {'domain': domain, 'value': {'selected_lines': []}}
预期的行为是只有与供应商id manyOne字段相关的项目
生成的行为是指检索到的所有项
编辑:
我注意到,当我删除widget=“section\u and\u note\u one2many”时,域可以完美地工作,但不能在同一表单中编辑树视图,即使使用editable=“bottom”也可以直接在域内添加使用供应商字段的检查 代码: 更新:
在
属性值\u id
上尝试实现与在/addons/mrp/models/mrp\u bom.py
中相同的功能。[版本:12]谢谢您的回复,但我的问题不在于供应商字段检查,需要做的是根据另一个字段的更改来更改检索到的值集,它在xml或python中都不起作用,我猜这是一个很多相关问题,因为如果我将many2many字段更改为many2one字段,同样的代码也可以工作。您使用的是哪个版本?在版本12、13中,域在许多字段上正常工作。使用odoo 12.0
@api.onchange('supplier_id')
def onchange_supplier(self):
domain = {'product_ids': []}
for rec in self:
if rec.supplier_id:
domain = {'product_ids': [('supplier_id', '=', rec.supplier_id.id)]}
return {'domain': domain}