from odoo import models, fields, api class Hostel(models.Model): _name = 'hostel.hostel' _description = '宿舍信息' _order = "id desc, name" _rec_name = 'hostel_code' _rec_names_search = ["name", "hostel_code"] currency_id = fields.Many2one('res.currency', string='Currency') # _table = 'hostel_hostel' custom_domain = "hostel_code" name = fields.Char(string='宿舍名称', required=True) hostel_code = fields.Char(string='代码', required=True) street = fields.Char('街道') street2 = fields.Char('街道2') city = fields.Char('城市') zip = fields.Char('邮编') state_id = fields.Many2one('res.country.state', string='省份') country_id = fields.Many2one('res.country', string='国家') phone = fields.Char('电话') mobile = fields.Char('手机') email = fields.Char('邮箱') hostel_floors = fields.Integer(string="楼层") image = fields.Binary("宿舍图片") active = fields.Boolean(string="可用", default=True, help="激活/停用 宿舍记录") type = fields.Selection( string="类型", selection=[("男性", "男生宿舍"), ("女性", "女生宿舍"), ("混合", "混合宿舍")], help="宿舍类型", required=True, default="混合" ) other_info = fields.Text(string="其他信息", help="输入更多信息") description = fields.Html(string="描述") hostel_rating = fields.Float('宿舍平均排名', digits="rating value") company_dependent = fields.Boolean(string="公司依赖", default=True) @api.depends('hostel_code') def _compute_display_name(self): for record in self: name = record.name if record.hostel_code: name = f"{name} ({record.hostel_code})" record.display_name = name @api.constrains('hostel_floors') def _check_hostel_floors(self): self.ensure_one() for record in self: if record.hostel_floors <= 0: raise models.ValidationError("楼层数不能小于0") elif record.hostel_floors > 50: # record.hostel_floors = 50 raise models.ValidationError("楼层数不能超过10层") # record.message_post(body="楼层数不能超过50层") # 此函数用于在当前字段分组时扩展read_group结果 # @api.model # def _read_group_selection_field(self, values, domain, order): # return ["choice1", "choice2"] # 此函数也用于在当前字段分组时扩展read_group结果 # @api.model # def _read_group_many2one_field(self, records, domain, order): # return records + self.search([custom_domain])