如何检查字段 'contains' Odoo 中的值

How to check if field 'contains' the value in Odoo

我正在尝试为特定类别的观众提供访问权限。

    <record id="rule_cost_centre_viewer" model="ir.rule">
        <field name="name">Access for the records for a cost_centre's viewer</field>
        <field name="model_id" ref="model_example_module"/>
        <field name="domain_force">[('user_id', 'in', category.viewers.ids)]</field>
        <field name="groups" eval="[(4,ref('group_example_module_user'))]"/>
    </record>

尝试失败,得到:

ValueError: <type 'exceptions.NameError'>: "name 'category' is not defined" while u"[('user_id', 'in', category.viewers.ids)]"

为了清楚起见:

class Example(models.Model):
    _name = 'example.module'
    category = fields.Many2one('example.category', 'Category')

class Category(models.Model):
    _name = 'example.category'
    name = fields.Char('Category')
    viewers = fields.Many2many('res.users', 'example_category_rel', 'user_id', 'viewer_id', 'Viewers')

这里有什么问题?

也许可以检查一下。

<field name="domain_force">[('category.viewers.ids', 'contains', user.id)]</field>

因为另一种方式不起作用?..

应该是:

<field name="domain_force">[('category.viewers', '=', user.id)]</field>

也许这个解决方案有点笨拙,但它确实有效。回答了类似的问题

并尽量遵循 Odoo 的字段命名指南:category_idviewer_ids 等等。