延续前日介绍,今天我们来讲Model内的fields延续昨日范例
# -*- coding: utf-8 -*-
from odoo import api, models, fields
class ResStudent(models.Model):
_name = 'res.student'
_inherit = 'res.partner'
_description = 'Student'
nickname = fields.Char(string='绰号')
math_score = fields.Float(string='数学成绩')
chinese_score = fields.Float(string='国文成绩')
avg_score = fields.Float(string='学期平均', compute='_compute_score')
birthday = fields.Date(string='生日', required=True)
school_id = fields.Many2one('res.company', string='所属学校')
school_city = fields.Char(string='所在城市', related='school_id.city')
senior_id = fields.Many2one('res.student', string='直属学长姐')
junior_ids = fields.One2many('res.student', 'senior_id', string='直属学弟妹')
teacher_ids = fields.Many2many('res.teacher', string='指导老师', domain=[('is_active', '=', True)])
gender = fields.Selection([("male", "男"), ("female", "女"), ("other", "其他")], string='性别')
is_leadership = fields.Boolean(default=False)
@api.depends('math_score', 'chinese_score')
def _compute_total(self):
for record in self:
record.avg_score = (record.math_score + record.chinese_score) / 2
Fields对应的就是资料表内的栏位,我们可以设定Fields的型别以及内部属性
True
or False
gender
,内部的选项也可以拉出来写。school_id
,通常会以_id
为命名结尾,代表多个学生会对应到一个学校,其中学校是以res_company
建立的model。junior_ids
,通常以_ids
为命名结尾,代表一位学生有多个直属学弟妹。teacher_ids
,命名同One2many,以_ids
结尾,代表多位学生会对应到多位指导老师。在宣告Fields的时候可以带参数去控制属性,如栏位显示名称、初始值、是否必填...等,有部分属性是在指定型别才可以加,以下介绍常用的几项属性:
通用栏位:
False
。True
时代表无法写入及修改。False
。readonly
, required
, invisible
三项参数在UI上的显示。True
,但One2many
栏位和计算栏位预设为False
。False
。readonly=True
、store=False
,如范例中的avg_score
此栏位透过_compute_total
方法计算出栏位的值,本身不会储存於db内,除非设定store=True
。school_city
便是参考school_id
底下city的值。关联栏位:
即是One2many
、Many2one
、Many2many
三种fields会出现相关关联参数:
school_id
参考的即是res_company
。One2many
、Many2one
栏位,可以想成是一个Filter,可透过domain来筛选想要的资料,如范例中的teacher_ids
透过filter,我们在指定给学生老师的时候只会选到is_active=True
的老师。set null
、restrict
、cascad
三种状态分别代表设成Null、无法删除、删除关联资料。ODOO会将预设栏位增加在model里,也就是每个table中都会有此栏位:
透过ODOO model的设定,ORM可以帮我们做很多db操作,省去不少麻烦,更多fields设定在ODOO Document也有解说,明天我们来介绍Model下的methods。
>>: [火锅吃到饱-6] 天圆地方职人锅物(台中)- 超高CP值的商业午餐 Business Set
这个得上一篇在https://ithelp.ithome.com.tw/articles/10257...
流程指派这件事情,还会依据流程关卡不同,有不同方向走法 像下图用自动节点指向两种路线 同样套用程序J...
在这30天里我自己也学到了很多,从一开始的基础元件使用,到後面的资料库、触控感测等较有难度的程序,但...
前言 JS 30 是由加拿大的全端工程师 Wes Bos 免费提供的 JavaScript 简单应用...
从这篇开始,我们要介绍一些Windows系统常识,做为一个资工先了解Windows的系统结构,懂得问...