from odoo import models, fields, api class Hostel(models.Model): _name = 'hostel.hostel' _description = 'Information about hostel' _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' 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("宿舍图片") activate = 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") @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): 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("楼层数不能超过50层") # record.message_post(body="楼层数不能超过50层")