- 字符:Char
Char对应pg的字段类型为变长的VARCHAR
char: 字符型,常用来存储长度小的文本
例子:单行文本
name = fields.Char(string='单据编号',required=True)
- 文本:text
Text对应pg的字段类型为长文本类型的text,多用于多行文本框,可以用widget属性为它添加样式。
note = fields.Text(string='备注', tracking=True)
- 布尔型:Boolean
布尔型:值为True 或False
active = fields.Boolean(default=True, string='是否归档', tracking=True)
- 整数 :integer
string:字段标签
sequence = fields.Integer(string='排序', default=50)
- 浮点型 :float
Float对应pg的字段类型为8个字节的double或变长的numeric,并且能执行sum聚合操作,可选参数digits,该字段是一个指定字段精度的(x,y)元组,x 是数字总长,y 是小数位,格式:digits=“[16,2]” 意思是 16位整数,2位小数
price_unit = fields.Float(string='单价',default=0.0,digits='Product Price')
- 日期 :date
Date对应pg的字段类型为日期类型的date
短日期:年月日
date = fields.Date(string='截止日期', tracking=True, default=fields.Date.today)
小贴示:
static today() – 以ORM期望的格式返回当前日期
static context_today() – 返回一个客户端时区的当前日期,可以接收一个datetime格式的参数
static to_date() – 将一个值转换成date对象
static to_string() – 将一个date或datetime对象转换成string
- 日期时间 :datetime
Datetime对应pg的字段类型为无时区的日期时间类型的timestamp
长日期,时间戳
top_time = fields.Datetime(string='置顶', default=fields.Datetime.now())
today = datetime.date.today() - datetime.timedelta(days=3)
date = datetime.datetime.strptime(kw[key], '%Y-%m-%d %H:%M:%S')
小贴示:
static add – 添加一个值
static context_timestamp
static now – 以ORM期望的格式返回当前日期和时间
static to_datetime()
static to_string()
static today()
- 二进制 :binary
Binary对应pg的字段类型为二进制类型bytea,用于保存图片、视频、文件、附件等,base64编码字符串存储,底层对该类型字段的容量作了限制,最多能容纳20M内容。
files = fields.Binary(string='文件', filters='*.xlsx', required=True)
简版格式:
快捷键:
required | 字段必填,这通过在数据库层面为列添加NOT NULL 约束来实现。 |
---|---|
readonly | 字段只读,在 API 层面并没有强制,模型方法中的代码仍然可以向其写入。仅针对用户界面设置。 |
invisible | 隐藏 |
index | 索引,为字段添加数据库索引,让搜索更快速,但同时也会部分降低写操作速度,默认为False。 |
help | 帮助文本:可以当前字段的用途,注意事项等 |
- 货币 :monetary
Monetary 对应pg的字段类型为变长的numeric,并且能执行sum聚合操作,但带有货币的特殊处理。第二个参数currency_field用于存储所使用货币,默认应传入currency_id字段
price_subtotal = fields.Monetary(compute='_compute_amount', string='小计', readonly=True)
- 副文本:Html:
Html对应pg的字段类型为长文本类型的text; Html是特殊的Text。该字段能保存html代码,与text类似,用于多行文本编辑,自带编辑器样式,并且会把内容以html解析。
article = fields.Html(string='文章')