Search code examples
odooodoo-8odoo-9odoo-10

Odoo: get type of field by name


in odoo you can get value of field by it's str name: exm:

  name = getattr(self, 'name')

what i want now is to know the type of field name is it :

fields.Char, fields.Many2one, fields.Many2many .....

so what i need is something like this

 gettype(self, 'user_id')

is there a way to now what is the type of field in odoo?


Solution

  • Odoo provides this information in the _fields attribute, I think It's better because every thing happens In the Python side no need for contacting the database, especially In my case my model have more than 30 fields :

     for name, field in self._fields.iteritems():
          if not isinstance(field, (fields.Many2one, fields.Many2many, fields.One2many)):
               # logic go here
    

    If you you want to verify just one fields:

      if not isinstance(self._fields[field_name], (fields.Many2one, ...)):  # do something