Odoo:按名称获取字段类型
Odoo: get type of field by name
在 odoo 中,您可以通过它的 str 名称获取字段的值:
例如:
name = getattr(self, 'name')
我现在想知道的是字段名称的类型:
fields.Char, fields.Many2one, fields.Many2many .....
所以我需要的是这样的东西
gettype(self, 'user_id')
现在有什么方法可以知道 odoo 中字段的类型吗?
Odoo 在 _fields
属性中提供此信息,我认为这更好,因为每件事都发生在 Python
方面,无需联系数据库,尤其是在我的情况下,我的模型有超过30
个字段:
for name, field in self._fields.iteritems():
if not isinstance(field, (fields.Many2one, fields.Many2many, fields.One2many)):
# logic go here
如果您只想验证一个字段:
if not isinstance(self._fields[field_name], (fields.Many2one, ...)): # do something
您可以从ir.model.fields型号搜索。
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"
这可能对你有帮助。
在 odoo 中,您可以通过它的 str 名称获取字段的值: 例如:
name = getattr(self, 'name')
我现在想知道的是字段名称的类型:
fields.Char, fields.Many2one, fields.Many2many .....
所以我需要的是这样的东西
gettype(self, 'user_id')
现在有什么方法可以知道 odoo 中字段的类型吗?
Odoo 在 _fields
属性中提供此信息,我认为这更好,因为每件事都发生在 Python
方面,无需联系数据库,尤其是在我的情况下,我的模型有超过30
个字段:
for name, field in self._fields.iteritems():
if not isinstance(field, (fields.Many2one, fields.Many2many, fields.One2many)):
# logic go here
如果您只想验证一个字段:
if not isinstance(self._fields[field_name], (fields.Many2one, ...)): # do something
您可以从ir.model.fields型号搜索。
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"
这可能对你有帮助。