from odoo import fields, models class Book(models.Model): _inherit = 'library.book' is_available = fields.Boolean('Is Available?') # 为isbn字段添加一条提示,说明同时支持10位数和13位数的 ISBN,稍后会实现该功能 isbn = fields.Char(help="Use a valid ISBN-13 or ISBN-10.") # 为publisher_id字段添加数据库索引,以提升搜索效率 publisher_id = fields.Many2one(index=True) def _check_isbn(self): self.ensure_one() isbn = self.isbn.replace('-', '') digits = [int(x) for x in isbn if x.isdigit()] if len(digits) == 10: ponderators = [1, 2, 3, 4, 5, 6, 7, 8, 9] total = sum(a * b for a, b in zip(digits[:9], ponderators)) check = total % 11 return digits[-1] == check else: # super()来调用父类已实现的方法 return super()._check_isbn()