如何在 <filter> 域中使用环境

How to use Environnement in <filter> domain

使用 Odoo 10(从 GitHub 提交 7413b26、分支 10.0 获取),我尝试在应用于树视图的搜索过滤器域中使用环境。像这样:

<filter string="Metal" domain="[('attribute_id','=', self.env['ir.config_parameter'].get_param('my_module.attrib_metal', None))]" help="metal"/>

但是Odoo不让我用self:

NameError: name 'self' is not defined


我还尝试通过将结果保存在一个字段中来使用 Python 过滤我的 attrib_metal,但我不想将其存储在我的数据库中,但是 Odoo 搜索要求这样做。

在我的 Python:

def get_my_params(self):
    attrid = self.env['ir.config_parameter'].get_param(
            'my_module.attrib_metal', None)
    if attrid:
        for rec in self:
            setattr(rec, attrib_metal, rec.attribute_id.id == int(attrid))

attrib_metal = fields.Boolean(store=False, compute="get_cr_params")

此代码是继承 product.attribute.value 模型的一部分。

在我的 XML 视图中:

<filter string="Metal" domain="[('attrib_metal', '=', 'True')]" help="metal"/>

我已经尝试使用 store = True 参数,但是这种方式将值存储在数据库中并且只计算 一次 ,这不是我想要的正在寻找。

所以这不是正确的做法。


如何使用 <filter> 域中的环境?

正在 Web 客户端中评估域 (frontend/Javascript)。 Web 客户端对 self 一无所知。此外,不支持过滤器中的点符号。 您将需要为此创建一个新字段,因为域在 javascript 一侧是 运行,您不能在那里执行代码。新字段还必须存储在数据库中。这种模式在整个 Odoo 中都被使用。

使用: <field name="your_field" invisible="1" /> 将其显示在您的视野中并使其不可见,然后

<filter string="Metal" domain="[('attribute_id','=', your_field]" help="metal"/>