Search code examples
pythonpython-2.7odooodoo-8

Error domain on odoo field


Problem:

On my main model I've got a many2one field that points to my submodel. I Wanted to filter the types available to select for the user with a calculated domain. However it gives me an error that the method doesn't exist. Does someone know how to fix this error?

Code example:

class my_class(models.Model):    
    _name = "my.model"
    name = fields.Char('Name', required=True)
    issue_type_id = fields.Many2one('my.model.type',"My Model Type", domain='_search_my_model_types', required=True)

    @api.model
    def _search_my_model_types(self):
        my_model_type_ids = []
        return [('id', 'in', my_model_type_ids)]

class my_class_type(models.Model):
    _name = "my.model.type"
    name = fields.Char("Name")

The error:

Uncaught Error: NameError: name '_search_my_model_types' is not defined

Solution

  • Try this:

    class my_class(models.Model):    
        _name = "my.model"
    
        #method definition before use
        @api.model
        def _search_my_model_types(self):
            my_model_type_ids = []
            return [('id', 'in', my_model_type_ids)]
    
        name = fields.Char('Name', required=True)
        #domain function not as a string
        issue_type_id = fields.Many2one('my.model.type',"My Model Type", domain=_search_my_model_types, required=True)