我看到了两种简单的可能性:
1. Odoo的继承使用
Odoo 的继承机制也在基础模型上工作。以下示例来自 Odoo V13 模块 base_import,但在 Odoo V11 中也有一些示例(例如在 web 中):
class Base(models.AbstractModel):
_inherit = 'base'
@api.model
def get_import_templates(self):
"""
Get the import templates label and path.
:return: a list(dict) containing label and template path
like ``[{'label': 'foo', 'template': 'path'}]``
"""
return []
以下是您需要的代码(在以后的版本中将无法使用,因为create 已更改):
class Base(models.AbstractModel):
_inherit = 'base'
def create(self, values):
"""
Extended core method to implement a license check,
before creating a record.
"""
check_license(self)
return super().create(values)
2。使用 mixin(Odoo 代码中有很多示例)
可以使用 mixin 类将许可证检查限制为某些模型,这些模型将继承此 mixin。 1. 概念并没有限制它(但也有可能)。
class LicenseCheckMixin(models.AbstractModel):
_name = 'license.check.mixin'
def create(self, values):
"""
Extended create method to implement a license check,
before creating a record.
"""
check_license(self)
return super().create(values)
然后您可以在其他 Odoo 模型中继承此 mixin:
class SaleOrder(models.Model):
_name = 'sale.order'
_inherit = ['sale.order', 'license.check.mixin']