class EpidemicRecord(models.Model): _name = ‘epidemic.record‘ # 表名 name = fields.Char(string=‘姓名‘) # string是用于前端显示的名字 date = fields.Date(string=‘确诊日期‘) state = fields.Char(string=‘省‘) city = fields.Char(string=‘市‘) county = fields.Char(string=‘区/街道‘) street = fields.Char(string=‘具体地址‘) ill_type = fields.Char(string=‘感染方式‘) within_or_abroad = fields.Selection([(‘within‘, ‘境内‘), (‘abroad‘, ‘*‘)], string=‘境内/*感染‘) # 枚举类型 is_ill = fields.Boolean(string=‘是否确诊‘, default=False) begin_lsolation_date = fields.Date(string=‘起始隔离时间‘) lsolation_mode = fields.Selection([(‘home‘, ‘居家隔离‘), (‘focus‘, ‘集中隔离‘)], string=‘隔离方式‘) create_user_id = fields.Many2one(‘res.users‘, string=‘填报人‘, default=lambda self: self.env.uid) # 外键关联,default是默认内容,这个函数的意思是默认值为当前登录用户 fuzhu_create_user_id = fields.Many2many(‘res.users‘, ‘epidemic_2_user‘, column1=‘epidemic_id‘, column2=‘user_id‘, string=‘辅助填报人‘) note = fields.Text(string=‘说明‘) @api.model def create(self, vals_list): """新建的时候会调用""" res = super(EpidemicRecord, self).create(vals_list) return res @api.onchange(‘name‘, ‘state‘, ‘is_ill‘, ‘within_or_abroad‘) def onchange_note(self): """ 用于监听前端数据的改动,实时返回内容。类似于前端js的change操作 函数名可以自定义 @api.onchange(‘name‘, ‘state‘, ‘is_ill‘, ‘within_or_abroad‘) 这里面写的就是需要监听的字段 self.note 这个是就是要根据内容改变而变动的字段 """ self.note = f‘{self.state}省的{self.name}{"确诊" if self.is_ill else "隔离"}‘ @api.multi def unlink(self): """删除的时候会调用""" res = super(EpidemicRecord, self).unlink() return res @api.multi def myserch(self): """自定义按钮""" domain = [ (‘is_ill‘, ‘=‘, True) ] res = self.search(domain) print(res)
自定义按钮
<!-- form视图--> <record id="repidemic_record_form_view" model="ir.ui.view"> <field name="name">疫情记录</field> <field name="model">epidemic.record</field> <field name="arch" type="xml"> <form> <sheet> <header> <!-- 自定义按钮 string 是按钮上显示的文字 type object固定 name 点击按钮之后需要调用的自定义方法 --> <button string="测试" type="object" name="myserch"/> </header> <group> <group> <field name="name"/> <field name="is_ill" required="1"/> <field name="date" attrs="{‘invisible‘:[(‘is_ill‘,‘=‘,False)],‘required‘:[(‘is_ill‘,‘=‘,True)]}"/> <field name="ill_type" attrs="{‘invisible‘:[(‘is_ill‘,‘=‘,False)],‘required‘:[(‘is_ill‘,‘=‘,True)]}"/> <field name="within_or_abroad" attrs="{‘invisible‘:[(‘is_ill‘,‘=‘,False)],‘required‘:[(‘is_ill‘,‘=‘,True)]}"/> <field name="begin_lsolation_date" attrs="{‘invisible‘:[(‘is_ill‘,‘=‘,True)],‘required‘:[(‘is_ill‘,‘=‘,False)]}"/> <field name="lsolation_mode" attrs="{‘invisible‘:[(‘is_ill‘,‘=‘,True)],‘required‘:[(‘is_ill‘,‘=‘,False)]}"/> </group> <group> <field name="state"/> <field name="city"/> <field name="county"/> <field name="street"/> <field name="create_user_id" readonly="1"/> <field name="fuzhu_create_user_id" widget="many2many_tags" options="{‘no_create‘:True,‘no_edit‘:True}"/> </group> </group> <notebook> <page string="说明"> <field name="note"/> </page> </notebook> </sheet> </form> </field> </record>