Odoo 10技术问询:如何在树视图中使用两个关联模型
完善Odoo Appointment预约模型的实现建议
看起来你已经搭好了预约模型的基础框架,我来帮你完善这个模型,并给出一些贴合实际业务的实用建议,让它更好用:
完整的模型代码实现
from odoo import models, fields, api class Appointment(models.Model): _name = 'appointment' _description = '患者预约记录' _order = 'appointment_date desc' # 按预约日期倒序排列,方便查看最新预约 appointment_details = fields.Text(string='预约详情', required=True) appointment_date = fields.Date(string='预约日期', required=True) patient_user_id = fields.Many2one( 'res.users', string='患者', default=lambda self: self.env.user, # 默认当前登录用户(适合患者自主预约场景) domain="[('groups_id', 'in', [ref('base.group_portal')])]" # 仅筛选门户组用户(假设患者归属该组) ) # 新增状态字段,跟踪预约生命周期 state = fields.Selection( [('draft', '草稿'), ('confirmed', '已确认'), ('canceled', '已取消')], string='预约状态', default='draft', required=True ) # 可选:关联医生(如果业务需要) doctor_user_id = fields.Many2one( 'res.users', string='接诊医生', domain="[('groups_id', 'in', [ref('base.group_user')])]" # 仅筛选内部系统用户作为医生 ) # 系统默认字段,自动记录创建/更新时间 create_date = fields.Datetime(string='创建时间', readonly=True) write_date = fields.Datetime(string='最后更新时间', readonly=True) @api.constrains('appointment_date') def _check_appointment_date(self): """校验预约日期不能早于今日""" for rec in self: if rec.appointment_date < fields.Date.today(): raise models.ValidationError("预约日期不能早于当前日期!") def action_confirm_appointment(self): """确认预约的动作方法""" self.write({'state': 'confirmed'}) # 可选:添加邮件通知患者的逻辑 # 先在模块中创建邮件模板,再调用发送 # template = self.env.ref('your_module.email_template_appointment_confirmed') # template.send_mail(self.id, force_send=True) def action_cancel_appointment(self): """取消预约的动作方法""" self.write({'state': 'canceled'})
关键优化说明
- 字段友好化:把原字段名
Patient id改成患者,界面显示更直观;给模型添加了描述和排序规则,提升后台管理体验 - 默认值与范围控制:给
patient_user_id设置默认当前登录用户,适合患者自主预约的场景;通过domain限制可选用户范围,避免选择无关人员 - 状态管理:新增
state字段,清晰跟踪预约的草稿、确认、取消状态,方便业务流程管控 - 数据校验:通过约束方法确保预约日期不会是过去的日期,避免无效预约
- 可扩展性:预留了医生关联字段和邮件通知逻辑,方便后续根据业务需求扩展
配套视图配置示例(XML)
如果需要配置后台的表单和列表视图,可以参考以下代码,让模型的操作界面更易用:
<odoo> <!-- 表单视图 --> <record id="view_appointment_form" model="ir.ui.view"> <field name="name">appointment.form</field> <field name="model">appointment</field> <field name="arch" type="xml"> <form string="患者预约"> <sheet> <group> <field name="patient_user_id"/> <field name="doctor_user_id"/> <field name="appointment_date"/> <field name="state" readonly="1"/> </group> <notebook> <page string="预约详情"> <field name="appointment_details" colspan="4"/> </page> </notebook> <!-- 操作按钮 --> <div class="oe_button_box" name="button_box"> <button name="action_confirm_appointment" type="object" string="确认预约" class="oe_highlight" attrs="{'invisible': [('state', 'in', ['confirmed', 'canceled'])]}"/> <button name="action_cancel_appointment" type="object" string="取消预约" attrs="{'invisible': [('state', '=', 'canceled')]}"/> </div> </sheet> </form> </field> </record> <!-- 列表视图 --> <record id="view_appointment_tree" model="ir.ui.view"> <field name="name">appointment.tree</field> <field name="model">appointment</field> <field name="arch" type="xml"> <tree string="预约列表"> <field name="patient_user_id"/> <field name="doctor_user_id"/> <field name="appointment_date"/> <field name="state"/> </tree> </field> </record> </odoo>
内容的提问来源于stack exchange,提问作者user9471328
相关产品推荐
相关产品推荐

