shcool/student_organization/models/student_organization.py
2026-06-10 16:05:38 +08:00

75 lines
2.7 KiB
Python

from odoo import api, fields, models
class StudentOrganization(models.Model):
_name = 'student.organization'
_description = '学生架构'
_rec_name = 'full_name'
_order = 'parent_id, sequence, id'
_parent_store = True
_parent_order = 'sequence, name'
# 基本信息
name = fields.Char(string='名称', required=True, translate=True)
code = fields.Char(string='代码')
sequence = fields.Integer(string='序号', default=10)
org_type = fields.Selection([
('college', '学院'),
('major', '专业'),
('class', '班级'), #用法 domain="[('org_type', '=', 'class')]"
], string='类型', required=True)
# 层级关系
parent_id = fields.Many2one('student.organization', string='上级组织', ondelete='cascade')
child_ids = fields.One2many('student.organization', 'parent_id', string='下级组织')
parent_path = fields.Char(index=True)
# 完整名称(显示层级)
full_name = fields.Char(string='完整名称', compute='_compute_full_name', store=True)
# 负责人信息(学院用)
manager_id = fields.Many2one('res.users', string='负责人')
# 联系方式(学院用)
phone = fields.Char(string='电话')
mobile = fields.Char(string='手机')
email = fields.Char(string='邮箱')
address = fields.Text(string='办公地址')
# 专业专用字段
duration = fields.Integer(string='修业年限(年)', default=4)
degree = fields.Selection([
('bachelor', '学士'),
('master', '硕士'),
('doctor', '博士'),
], string='学位')
# 班级专用字段
grade = fields.Char(string='年级')
homeroom_teacher = fields.Many2one('hr.employee', string='导员')
# 统计字段
student_count = fields.Integer(string='学生数', compute='_compute_student_count')
@api.depends('parent_id.full_name', 'name')
def _compute_full_name(self):
for record in self:
if record.parent_id:
record.full_name = f'{record.parent_id.full_name} / {record.name}'
else:
record.full_name = record.name
def _compute_student_count(self):
for record in self:
if record.org_type == 'class':
# 班级的学生数
record.student_count = self.env['student.info'].search_count([('class_id', '=', record.id)])
else:
# 学院/专业的学生数(统计下级)
students = self.env['student.info'].search([
('org_path', 'like', f'{record.parent_path}%')
]) if record.parent_path else self.env['student.info'].search([])
record.student_count = len(students)