Search code examples
javascriptpythonodoo

Odoo - How to add custom field attribute without modifying by extending


I am trying to use my custom attribute for fields called sf_group. Unfortunately, this attribute is shown when getting field description via fields_get(). Is there a way to add this attribute to the field description? I need to check if this attribute is on the field and what's its value on the JS side.

Note: Odoo - add custom field attribute? didn't help because it only works if you modify the source code which is something I don't want to do.

Thanks!


Solution

  • Found an answer!

    from odoo import models, fields, api
    
    class Company(models.Model):
        _inherit = "res.company"
        test_field = fields.Char(string="Test Field", custom_attr="random01")
        def _valid_field_parameter(self, field, name):
            return name == 'custom_attr' or super()._valid_field_parameter(field, name)
        @api.model
        def fields_get(self, allfields=None, attributes=None):
            fields = super().fields_get(allfields=allfields, attributes=attributes)
            for key in fields.keys():
                if key in self._fields and hasattr(self._fields[key], 'custom_attr'):
                    fields[key]['custom_attr'] = self._fields[key].custom_attr
            return fields