模型实例:
class PersonInformation(models.Model):
_name = 'person.info'
_description = "Person Information"
name = fields.Char(string='名称', help='名称', required=True, index=True)
gender = fields.Selection([('male', '男'),('female', "女")], string='性别', required=True, index=True)
country = fields.Many2one(comodel_name='person.country', string='国家', required=True)
jobs = fields.Many2many('person.job', string='工作')
1. model中_fields属性:获取模型的字段
self._fileds
返回结果:OrderedDict字典类型值集
OrderedDict([(‘name’, person.info.name), (‘gender’, person.info.gender), (‘country’, person.info.country), (‘jobs’, person.info.jobs), (‘id’, person.info.id), (‘display_name’, person.info.display_name), (‘field_show’, person.info.field_show), (‘create_uid’, person.info.create_uid), (‘create_date’, person.info.create_date), (‘write_uid’, person.info.write_uid), (‘write_date’, person.info.write_date), (’__last_update’, person.info.__last_update)])
循环打印每一项可以看到:
for fname, field in self._fields.items():
description = field.get_description(self.env)
print(fname)
print(description)
name
{'type': 'char', 'change_default': False, 'company_dependent': False, 'depends': (), 'help': '名称', 'manual': False, 'readonly': False, 'required': True, 'searchable': True, 'sortable': True, 'store': True, 'string': '名称', 'translate': False, 'trim': True}
...
2. filed字段get_description方法获取字段的属性
field.get_description(self.env) 获取字段的描述
返回结果:
{'type': 'char', 'change_default': False, 'company_dependent': False, 'depends': (), 'help': '名称', 'manual': False, 'readonly': False, 'required': True, 'searchable': True, 'sortable': True, 'store': True, 'string': '名称', 'translate': False, 'trim': True}
源码 get_description 方法
def get_description(self, env):
""" Return a dictionary that describes the field ``self``. """
desc = {'type': self.type}
for attr, prop in self.description_attrs:
value = getattr(self, prop)
if callable(value):
value = value(env)
if value is not None:
desc[attr] = value
return desc