嵌套 x2many 字段上的域

Domain on nested x2many field

我认为这会更简单,但经过数小时的谷歌搜索和反复试验后,我...

在我的自定义模块中,我有一个名为笔记的选项卡(笔记本页面)。在此选项卡中是一个简单的 one2many 字段,因此我可以为此记录添加多个注释。笔记有一个文本字段和一个 many2many 标签字段,因此每个笔记都可以有标签。

如果有任何带有 "Warning" 标签的笔记,我会尝试显示红色 "Warning icon"(为简单起见,以智能按钮的形式)。我想我会隐藏它,除非有警告标签,我只是想不出我的 attrs="{'invisible':[('<field for tags>','in','Warning')]}"

的域

解决此问题的最佳方法是什么? 这是 x2many 字段的模型:

class sites_notes(models.Model):
    _name = 'sites.notes'
    _order = "write_date DESC"
    tower_id = fields.Many2one('sites.sites', string='Site')
    tag_id = fields.Many2many('sites.notes.tags', 'sites_notes_tags_rel', string="Tags")
    note = fields.Text('Notes')

class sites_notes_tags(models.Model):
    _name = 'sites.notes.tags'
    name = fields.Char('Tag', size=24)
    note_id = fields.Many2many('sites.notes', 'sites_notes_tags_rel', string="Note")

也许不是最好的方法,但却是可行的方法:

  1. 在定义了 x2many 字段的模型上创建一个 function/computed 字段(例如 has_warning)。
  2. 根据您的条件检查所有注释来计算布尔值。
  3. 将 function/compute 字段插入到您的视图定义中(可能是不可见的!)。
  4. 在该字段上显示带有域的警告按钮,例如:attrs="{'invisible':[('has_warning','!=',True)]}"