Search code examples
many-to-manyodooone-to-manyodoo-9

Domain on nested x2many field


I thought this would be simpler but after hours of googling and trial and error here I am...

In my custom module, I have a tab (notebook page) called notes. In this tab is a simple one2many field so I can add multiple notes for this record. Notes have a text field and a many2many tag field so each note can have tags.

I an trying to display a red "Warning icon" (in form of a smart-button for simplicity) if there is any note that has "Warning" tag. I figured I would hide it unless there is a warning tag, I just can't figure out the domain for my attrs="{'invisible':[('<field for tags>','in','Warning')]}"

What's the best way to go about this? Here is the models for the x2many fields:

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")

Solution

  • Perhaps not the best way, but a working way:

    1. Create an function/computed field (e. g. has_warning) on the model where the x2many field is defined.
    2. Compute boolean by checking all the notes for your condition.
    3. Insert the function/compute field into your view definitions (could be invsible!).
    4. Show your warning button with a domain on that field like: attrs="{'invisible':[('has_warning','!=',True)]}"