如何将外部 ID 传递给域过滤器

How to pass an external id to a domain filter

我的模型中有一个接受产品的 Many2One 字段,但我想使用上述模板外部 ID(XML ID)将此字段限制为特定产品模板。

我已经尝试过但没有成功:

#This piece of code doesn't work
the_product = fields.Many2one('product.product', 
                  domain = [('product_tmpl_id','=', "ref('the_package.the_external_id')")])

我该怎么做?

此问题的解决方案是使用 returns 过滤参数的函数。这样,我们就可以访问函数主体中的 self 变量,因此,我们可以使用它来搜索特定的外部 ID。

@api.model
def _get_domain(self):
    # We have access to self.env in this context.
    ids = self.env.ref('the_package.the_external_id').ids
    return [('product_tmpl_id','=', ids)]


the_field = fields.Many2one('product.product',
                             required=False, 
                             domain = _get_picking_product_domain)

上面的解决方案不起作用。

错误:

ProgrammingError: 无法调整类型 'model_name'

但是试试这个:

您不必在您的字段中插入域:

@api.onchange('field_a')
def fiel_change(self):
    dom['field_a'] = [(*)] #* type your domain  
    return {'domain':dom}