Charif DZ
Charif DZ

Reputation: 14751

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?

Upvotes: 1

Views: 3637

Answers (2)

Charif DZ
Charif DZ

Reputation: 14751

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

Upvotes: 4

You can search from ir.model.fields model.

ir_model_obj=self.env['ir.model.fields']
ir_model_field=ir_model_obj.search([('model','=',model),('name','=',field)])
field_type=ir_model_field.ttype
if field_type=='many2one':
    print "do operation"

This may help you.

Upvotes: 5

Related Questions