odoo17核心概念——env

2023-12-24 23:01
文章标签 概念 核心 env odoo17

本文主要是介绍odoo17核心概念——env,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

env在odoo中是一个非常重要的概念,它是一个全局变量,保存了odoo运行环境的重要信息,env分为前端和后端

一、环境(env)

1、前端的env

在web\static\src\env.js中定义,包含两个重要的对象:

  • 全局数据总线bus, 可以在不同的组件之间进行通信
  • 所有的服务services

这个文件非常重要,它定义了两个函数

makeEnv: 初始化env对象。

export function makeEnv() {return {bus: new EventBus(),services: {},debug: odoo.debug,get isSmall() {throw new Error("UI service not initialized!");},};
}

startServices: 启动所有的Service, 这个函数的实现非常巧妙,因为它要在不考虑服务加载顺序的前提下解决服务之间的依赖问题。 其中细节,值得一读。

不过这里只有这两个函数的定义,并没有执行这两个函数, 真正执行这两个函数的位置在 start.js中

const env = makeEnv();
await startServices(env);

服务启动完后, 会放入env的services对象中。 注意其中放的不是服务对象本身,而是start函数的返回值。

2、 后端的env

后端的env在odoo\api.py中定义

"""The Odoo API module defines Odoo Environments and method decorators... todo:: Document this module
"""__all__ = ['Environment','Meta','model','constrains', 'depends', 'onchange', 'returns','call_kw',
]

文件的注释中说明了两点:
1、这个文件定义了odoo环境和方法装饰器
2、todo: 要为这个模块写文档(啥时候写?)
默认导出的对象,odoo后端最重要的一些对象
‘Environment’,
‘Meta’,
‘model’,
‘constrains’, ‘depends’, ‘onchange’, ‘returns’,
‘call_kw’,

from collections.abc import Mappingclass Environment(Mapping):""" The environment stores various contextual data used by the ORM:- :attr:`cr`: the current database cursor (for database queries);- :attr:`uid`: the current user id (for access rights checks);- :attr:`context`: the current context dictionary (arbitrary metadata);- :attr:`su`: whether in superuser mode.It provides access to the registry by implementing a mapping from modelnames to models. It also holds a cache for records, and a datastructure to manage recomputations."""def reset(self):""" Reset the transaction, see :meth:`Transaction.reset`. """self.transaction.reset()def __new__(cls, cr, uid, context, su=False):if uid == SUPERUSER_ID:su = Trueassert context is not Noneargs = (cr, uid, context, su)# determine transaction objecttransaction = cr.transactionif transaction is None:transaction = cr.transaction = Transaction(Registry(cr.dbname))# if env already exists, return itfor env in transaction.envs:if env.args == args:return env# otherwise create environment, and add it in the setself = object.__new__(cls)args = (cr, uid, frozendict(context), su)self.cr, self.uid, self.context, self.su = self.args = argsself.transaction = self.all = transactionself.registry = transaction.registryself.cache = transaction.cacheself._cache_key = {}                    # memo {field: cache_key}self._protected = transaction.protectedtransaction.envs.add(self)return self

env对象存储了不同类型的上下文数据被ORM使用,
- :attr:cr: the current database cursor (for database queries);
- :attr:uid: the current user id (for access rights checks);
- :attr:context: the current context dictionary (arbitrary metadata);
- :attr:su: whether in superuser mode.

env继承自 collection 的Mapping

在文件的末尾有两句import有点意思

# keep those imports here in order to handle cyclic dependencies correctly
from odoo import SUPERUSER_ID
from odoo.modules.registry import Registry

之所以放在文件末尾,是为了正确的处理循环依赖。 姑且先不管的原理是啥了,不过这里引入了一个重要的对象 Regisitry ——后端的注册表

class Registry(Mapping):""" Model registry for a particular database.The registry is essentially a mapping between model names and model classes.There is one registry instance per database."""_lock = threading.RLock()_saved_lock = None@lazy_classpropertydef registries(cls):""" A mapping from database names to registries. """size = config.get('registry_lru_size', None)if not size:# Size the LRU depending of the memory limitsif os.name != 'posix':# cannot specify the memory limit soft on windows...size = 42else:# A registry takes 10MB of memory on average, so we reserve# 10Mb (registry) + 5Mb (working memory) per registryavgsz = 15 * 1024 * 1024size = int(config['limit_memory_soft'] / avgsz)return LRU(size)def __new__(cls, db_name):""" Return the registry for the given database name."""with cls._lock:try:return cls.registries[db_name]except KeyError:return cls.new(db_name)finally:# set db tracker - cleaned up at the WSGI dispatching phase in# odoo.http.rootthreading.current_thread().dbname = db_name

这里不想写的过于深入,先摸清系统的大概框架,在需要的时候再去扣细节,这才是学习的正确方法 。

这篇关于odoo17核心概念——env的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/533413

相关文章

Python进阶之列表推导式的10个核心技巧

《Python进阶之列表推导式的10个核心技巧》在Python编程中,列表推导式(ListComprehension)是提升代码效率的瑞士军刀,本文将通过真实场景案例,揭示列表推导式的进阶用法,希望对... 目录一、基础语法重构:理解推导式的底层逻辑二、嵌套循环:破解多维数据处理难题三、条件表达式:实现分支

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

一文带你迅速搞懂路由器/交换机/光猫三者概念区别

《一文带你迅速搞懂路由器/交换机/光猫三者概念区别》讨论网络设备时,常提及路由器、交换机及光猫等词汇,日常生活、工作中,这些设备至关重要,居家上网、企业内部沟通乃至互联网冲浪皆无法脱离其影响力,本文将... 当谈论网络设备时,我们常常会听到路由器、交换机和光猫这几个名词。它们是构建现代网络基础设施的关键组成

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

详解MySQL中DISTINCT去重的核心注意事项

《详解MySQL中DISTINCT去重的核心注意事项》为了实现查询不重复的数据,MySQL提供了DISTINCT关键字,它的主要作用就是对数据表中一个或多个字段重复的数据进行过滤,只返回其中的一条数据... 目录DISTINCT 六大注意事项1. 作用范围:所有 SELECT 字段2. NULL 值的特殊处

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

java中Optional的核心用法和最佳实践

《java中Optional的核心用法和最佳实践》Java8中Optional用于处理可能为null的值,减少空指针异常,:本文主要介绍java中Optional核心用法和最佳实践的相关资料,文中... 目录前言1. 创建 Optional 对象1.1 常规创建方式2. 访问 Optional 中的值2.1

MySQL 事务的概念及ACID属性和使用详解

《MySQL事务的概念及ACID属性和使用详解》MySQL通过多线程实现存储工作,因此在并发访问场景中,事务确保了数据操作的一致性和可靠性,下面通过本文给大家介绍MySQL事务的概念及ACID属性和... 目录一、什么是事务二、事务的属性及使用2.1 事务的 ACID 属性2.2 为什么存在事务2.3 事务