浏览代码

继承 Python 方法,对业务逻辑添加功能

Your Name 2 月之前
父节点
当前提交
c8bd4e981d
共有 1 个文件被更改,包括 13 次插入1 次删除
  1. 13 1
      cat_app/library_member/models/library_book.py

+ 13 - 1
cat_app/library_member/models/library_book.py

@@ -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()