字段属性对象Field
Model的字段属性对应数据表中的相应字段,pg表中不同的字段有不同类型,Odoo也为其封装了相应的类型对象
Field类型的属性:
- type = None 字段类型
- relational = False 是否是关联字段
- translate = False 字段是否翻译
- column_type = None 数据库字段类型
- column_format = '%s' 字段提示信息
- _slots = {
'args': EMPTY_DICT, # init()的设置参数
'_attrs': EMPTY_DICT, # the field's non-slot attributes
'_module': None, # 字段的模块名称
'_setup_done': None, # 字段设置状态: None, 'base' or 'full'
'automatic': False, # 是否是自动创建的字段 ("magic" field)
'inherited': False, # 是否是继承的字段 (_inherits)
'name': None, # 字段名称
'model_name': None, # 字段所在的模型名称
'comodel_name': None, # 关联字段的模型名称
'store': True, # 字段值是否保存到数据库
'index': False, # 是否在数据库中为字段添加索引
'manual': False, # 是否是自定义字段
'copy': True, # 字段值是否可以复制
'depends': (), # 字段的依赖集合
'recursive': False, # 是否自己依赖自己
'compute': None, # 计算字段的计算方法
'compute_sudo': False, # 字段是否以admin重新计算
'inverse': None, # inverse(recs) inverses field on recs
'search': None, # search(recs, operator, value) searches on self
'related': None, # 依赖字段的名称
'related_sudo': True, # whether related fields should be read as admin
'company_dependent': False, # whetherself
is company-dependent (property field)
'sparse': None, # the name of the corresponding serialized field, if any
'default': None, # 字段默认值
'string': None, # 字段说明
'help': None, # 字段提示
'readonly': False, # 是否只读
'required': False, # 是否必填字段
'states': None, # set readonly and required depending on state
'groups': None, # csv list of group xml ids
'change_default': False, # whether the field may trigger a "user-onchange"
'deprecated': None, # whether the field is deprecated
'related_field': None, # corresponding related field
'group_operator': None, # 能够执行聚合的操作
'group_expand': None, # name of method to expand groups in read_group()
'prefetch': True, # whether the field is prefetched
}
Odoo封装的字段对象主要有:
- Boolean
- Integer
- Float
- Monetary
- Char
- Text
- Html
- Date
- Datetime
- Binary
- Selection
- Reference
- Many2one
- One2many
- Many2many
Boolean
Boolean字段的type值为boolean;column_type值为('bool', 'bool')
Boolean对应pg的字段类型为boolean
例:
active = fields.Boolean(default=True, help=u"设置字段是否有效")
Integer
Integer字段的type值为integer;column_type值为('int4', 'int4')
_slots = { 'group_operator': 'sum', }
Integer对应pg的字段类型为4个字节的integer,并且能执行sum的聚合操作
例:
sequence = fields.Integer(string=u'序号', default=1)
Float
Float字段type值为float;column_type值为('numeric', 'numeric')或('float8', 'double precision')
_slots = {'_digits': None, 'group_operator': 'sum',}
Float对应pg的字段类型为8个字节的double或变长的numeric,并且能执行sum聚合操作
例:
product_qty = fields.Float(string=u'产品数量', digits=dp.get_precision('Product Unit of Measure'), required=True, index=True)
Monetary
Monetary字段的type值为monetary;column_type值为('numeric', 'numeric')
_slots = {'currency_field': None, 'group_operator': 'sum', }
Monetary 对应pg的字段类型为变长的numeric,并且能执行sum聚合操作
例:
amount = fields.Monetary(string=u'金额', currency_field='company_currency_id')
Char
Char字段的type值为char;column_type值为('varchar', 'VARCHAR')
Char对应pg的字段类型为变长的VARCHAR
例:
name = fields.Char(string=u'名称', index=True, required=True)
Text
Text字段的type值为text;column_type值为('text', 'text')
Text对应pg的字段类型为长文本类型的text
例:
comment = fields.Text(string=u'评论')
Html
Html字段的type值为text;column_type值为('text', 'text')
Html对应pg的字段类型为长文本类型的text; Html是特殊的Text该字段能保存html代码
例:
comment = fields.Html(string=u'评论')
Date
Date字段的type值为date;column_type值为('date', 'date')
Date对应pg的字段类型为日期类型的date
例:
start_date = fields.Date(string=u'开始日期', default=fields.Date.today)
Datetime
Datetime字段的type值为datetime;column_type值为('timestamp', 'timestamp')
Datetime对应pg的字段类型为无时区的日期时间类型的timestamp
例:
start_time = fields.Date(string=u'开始时间', default=fields.Date.now)
Binary
Binary字段的type值为binary;column_type值为('bytea', 'bytea')
Binary对应pg的字段类型为二进制类型bytea
Selection
Selection字段的type值为selection;column_type值为('int4', 'integer')或('varchar', 'VARCHAR')
Selection对应pg的字段类型为4个字节的interger或者是不定长的varchar
例:
state = fields.Selection(selection=[('draft',u'草稿'), ('done', u'完成')], default='draft')
Reference
Reference字段的type值为reference;column_type值为('varchar', 'VARCHAR')
Reference对应pg的字段类型为不定长的varchar
Many2one
Many2one字段的type值为many2one;column_type值为('int4', 'int4')
_slots = {'ondelete': 'set null', 'auto_join': False, 'delegate': False, }
Many2one是一个多对一的外表关联字段,对应pg的字段类型是4个字节的integer
例:
partner_id = fields.Many2one(comodel_name='res.partner', string=u'业务伙伴')
One2many
One2many字段的type值为one2many;
_slots = {'inverse_name': None, 'auto_join': False, 'limit': None,'copy': False,}
One2many是一个一对多的关联字段,与Manyone形成呼应
例:
stock_quant_ids = fields.One2many(comodel_name='stock.quant', inverse_name='product_id')
Many2many
Many2many字段的type值为many2many;
_slots = {'relation': None, 'column1': None, 'column2': None, 'auto_join': False, 'limit': None,}
many2many是一个多对多的关联字段
例:
sale_line_ids = fields.Many2many(comodel_name='sale.order.line', relation='sale_order_line_invoice_rel', column1='invoice_line_id', column2='order_line_id', string='Sale Order Lines', readonly=True)