2
votes

I'm trying to make a filter in a search view with a domain depending on the attribute of a specific record, whose XML ID I have. The problem is that I don't know how to get the object and use its attribute in that situation.

This is what I want, but it throws the error "ref unknown":

<filter string="My filter" name="my_filter" domain="[('my_field','=',ref('my_module.my_xml_id').name)]"/>

Using %(my_module.my_xml_id)d isn't a valid option since I can't get the attribute of an integer and I need the name of the object.

So I was wondering if there's a better solution than creating a stored computed field in the model to have the name available in the filters. It would be awful because I would be storing the same value in each record...

Any ideas? Thank you!

1

1 Answers

0
votes
class YourClass(models.Model):

    _name = 'your.class'

    @api.model
    def _your_field_domain(self):
        return [
            ('my_field', '=',
             self.env.ref('my_module.my_xml_id').name)
            ]

    your_field = fields.Many2one(
        'another.class',
        domain=_your_field_domain
    )