|
@@ -7,4 +7,16 @@ class Book(models.Model):
|
|
|
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()
|