模型和Python类

阅读(24) 最后更新: 2019年2月16日 17:41

上一页 模型属性 | 瞬态和抽象模型 下一页

翻译进度 100%

自助翻译

类标识符的约定是使用CamelCases,这遵循PEP 8编码约定定义的Python标准。
The convention for class identifiers is to use CamelCase.This follows the Python standard defined by the PEP8 coding conventions.
由于在其他模块中不存在与类发生冲突的风险,所以不需要以与类相关的主要应用程序作为类标识符的前缀。
Since there is no risk of collision with classes in other modules, there is no need for class identifiers to be prefixed by the main application they relate to.
另一方面,Python类是声明它们的Python文件的本地类,用于它们的标识符仅对该文件中的代码具有重要意义,并且很少相关。
Python classes, on the other hand, are local to the Python file where they are declared.The identifier used for them is only significant for the code in that file, and is rarely relevant.
模型名必须是全局唯一的。因此,第一个词应该对应于该模块所涉及的主要应用程序。对于我们的库应用功能,我们将在所有模型前加上库。其他来自核心模块的例子是项目、客户关系管理和销售。
Model names must be globally unique.Because of this, the first word should correspond to the main application the module relates to.For our Library app features, we will prefix all models with library.Other examples from the core modules are project, crm, and sale.
出于历史原因,有可能找到一些不遵循此规则的核心模型,例如res.user,但这不是规则。
For historical reasons, it is possible to find some core models that don't follow this, such as res.users, but this is not the rule.
我们应该对模型名使用单数形式:labary.book,而不是labary.book。
We should use the singular form for model names: library.book rather than library.books.
您可以看到,模型名称很重要,因为它们是用来访问注册表的键。模型名称的约定是使用与点连接的小写单词列表,例如Library.book或Library ary.book.范畴。来自核心模块的其他示例有project.project、project.Task和project.task.type。
You can see that model names are important since they are the keys used to access the registry.The convention for model names is to use a list of lowercase words joined with dots, such as library.book or library.book.category.Other examples from the core modules are project.project, project.task, and project.task.type.
具体来说,模型方法中的代码可以使用Sel.env[‘Library ary.book’]检索表示Library.book模型的模型类。
Specifically, the code in a model method can use self.env['library.book'] to retrieve the model class representing the library.book model.
Odoo模型保存在中央注册表中,在env环境对象(在旧API中称为池)中可用。它是一种字典,它保存对数据库中所有模型类的引用,它的条目可以通过模型名称引用。
Odoo models are kept in a central registry, available in the env environment object (referred to as a pool in the old API).It is a dictionary that keeps references to all the model classes available in the database, and its entries can be referenced by a model name.
Odoo模型由Python类表示,在前面的代码中,基于Models.Model类,我们有一个Python类Book,它定义了一个新的Odoo模型,名为Library.book。
Odoo models are represented by Python classes.In the preceding code, we have a Python class, Book, based on the models.Model class, which defines a new Odoo model called library.book.

odoo技术交流2 群号 625337975

http://www.odoogo.com/post/58/Odoo开发视频教程

文档不断更新中,添加微信odoogo,获取最新视频课程和模块

留言提问