hostel.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from odoo import models, fields, api
  2. class Hostel(models.Model):
  3. _name = 'hostel.hostel'
  4. _description = 'Information about hostel'
  5. _order = "id desc, name"
  6. _rec_name = 'hostel_code'
  7. _rec_names_search = ["name", "hostel_code"]
  8. currency_id = fields.Many2one('res.currency', string='Currency')
  9. # _table = 'hostel_hostel'
  10. custom_domain = "hostel_code"
  11. name = fields.Char(string='宿舍名称', required=True)
  12. hostel_code = fields.Char(string='代码', required=True)
  13. street = fields.Char('街道')
  14. street2 = fields.Char('街道2')
  15. city = fields.Char('城市')
  16. zip = fields.Char('邮编')
  17. state_id = fields.Many2one('res.country.state', string='省份')
  18. country_id = fields.Many2one('res.country', string='国家')
  19. phone = fields.Char('电话')
  20. mobile = fields.Char('手机')
  21. email = fields.Char('邮箱')
  22. hostel_floors = fields.Integer(string="楼层")
  23. image = fields.Binary("宿舍图片")
  24. active = fields.Boolean(string="可用", default=True, help="激活/停用 宿舍记录")
  25. type = fields.Selection(
  26. string="类型",
  27. selection=[("男性", "男生宿舍"), ("女性", "女生宿舍"), ("混合", "混合宿舍")],
  28. help="宿舍类型",
  29. required=True,
  30. default="混合"
  31. )
  32. other_info = fields.Text(string="其他信息", help="输入更多信息")
  33. description = fields.Html(string="描述")
  34. hostel_rating = fields.Float('宿舍平均排名', digits="rating value")
  35. company_dependent = fields.Boolean(string="公司依赖", default=True)
  36. @api.depends('hostel_code')
  37. def _compute_display_name(self):
  38. for record in self:
  39. name = record.name
  40. if record.hostel_code:
  41. name = f"{name} ({record.hostel_code})"
  42. record.display_name = name
  43. @api.constrains('hostel_floors')
  44. def _check_hostel_floors(self):
  45. for record in self:
  46. if record.hostel_floors <= 0:
  47. raise models.ValidationError("楼层数不能小于0")
  48. elif record.hostel_floors > 50:
  49. # record.hostel_floors = 50
  50. raise models.ValidationError("楼层数不能超过50层")
  51. # record.message_post(body="楼层数不能超过50层")
  52. # 此函数用于在当前字段分组时扩展read_group结果
  53. # @api.model
  54. # def _read_group_selection_field(self, values, domain, order):
  55. # return ["choice1", "choice2"]
  56. # 此函数也用于在当前字段分组时扩展read_group结果
  57. # @api.model
  58. # def _read_group_many2one_field(self, records, domain, order):
  59. # return records + self.search([custom_domain])