admin管理员组

文章数量:1122846

I need to create a new Many2one field in Odoo 17, extending the standard res.partner model so that the domain of the newly created res_partner.settore_principale field and its possible values are selected dynamically according to the value of the res_partner.x_studio_macrocategoria attribute of the same record, both in case the latter is modified and in case it hasn't been.

I tries to create a new field with a callback defining it's domain, as shown below:

    settore_principale = fields.Many2one(
        'res.partner.industry', 
        string="Settore",
        domain=lambda self: self._compute_settore_domain()  # I tried both with and without str()
    )
    
    def _compute_settore_domain(self):
        domain = [('x_studio_settore_padre', '=', False)]
        if self.x_studio_macrocategoria: 
            domain.append(('x_studio_macrocategoria', '=', self.x_studio_macrocategoria))
        return domain  # Return domain as a list of tuples, not string

    @api.onchange('x_studio_macrocategoria')
    def _onchange_macrocategoria(self):
        return {'domain': {'settore_principale': self._compute_settore_domain()}}

fact is that on loading the addon, even on a first try, I receive the following error:

odoo.addons.base.models.ir_qweb.QWebException: Error while render the template
UndefinedColumn: column res_partner.settore_principale does not exist
LINE 1: ...uid", "res_partner"."write_date" AS "write_date", "res_partn...
                                                             ^

it appears that while the code gets parsed and executed in at least a partially correct way, it doesn't allow Odoo to follow with the actual field creation in the database.

Could anyone help me understand what i'm doing wrong?

Thanks in advance

本文标签: pythonDynamic many2one field domain in Odoo 17 based on callback functionStack Overflow