我假设它用于引用openerp中其他模块中的字段,但我不确定.
在这里,他们以某种方式从一个产品模块到销售模块获得价格.
price = self.pool.get('product.pricelist').price_get(cr, uid, [pricelist], product, qty or 1.0, partner_id, ctx)[pricelist]
if price is False:
warn_msg = _("Cannot find a pricelist line matching this product and quantity.\n"
"You have to change either the product, the quantity or the pricelist.")
warning_msgs += _("No valid pricelist line found ! :") + warn_msg +"\n\n"
else:
result.update({'price_unit': price})
if context.get('uom_qty_change', False):
return {'value': {'price_unit': price}, 'domain': {}, 'warning': False}
Run Code Online (Sandbox Code Playgroud)
self.pool.get()
在pool这里只是被用来存储的情况下,类似字典的对象OpenERP models,如res.users或ir.model.data.
OpenERP/Odoo的多数据库特性:单个OpenERP服务器进程可以管理多个数据库,对于每个数据库,已安装模块的集合以及模型集可以有所不同.
所以,我们有不同的pools,每个数据库一个,以及引用我们已经在的模型实例的常规方法,self
感谢The Hypered Blog对该网站的精彩解释self.pool.get().有关更多信息,请检查该链接.
水池
池是一个注册表(字典对象{})。
注册表本质上是模型名称和模型实例之间的映射。每个数据库有一个注册表实例。
服务器/openerp/osv/orm.py
class BaseModel(object):
def __init__(self, pool, cr):
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)""" Initialize a model and make it part of the given registry. - copy the stored fields' functions in the osv_pool, - update the _columns with the fields found in ir_model_fields, - ensure there is a many2one for each _inherits'd parent, - update the children's _columns, - give a chance to each field to initialize itself. """
pool.add(self._name, self)
self.pool = pool
Run Code Online (Sandbox Code Playgroud)
参考/openerp/sql_db.py查看Odoo-Postgresql 连接池如何建立。
_Pool = None
def db_connect(to, allow_uri=False):
global _Pool
if _Pool is None:
_Pool = ConnectionPool(int(tools.config['db_maxconn']))
db, uri = dsn(to)
if not allow_uri and db != to:
raise ValueError('URI connections not allowed')
return Connection(_Pool, db, uri)
def close_db(db_name):
""" You might want to call openerp.modules.registry.RegistryManager.delete(db_name) along this function."""
global _Pool
if _Pool:
_Pool.close_all(dsn(db_name)[1])
def close_all():
global _Pool
if _Pool:
_Pool.close_all()
Run Code Online (Sandbox Code Playgroud)
连接等级
class Connection(object):
""" A lightweight instance of a connection to postgres
"""
def __init__(self, pool, dbname, dsn):
self.dbname = dbname
self.dsn = dsn
self.__pool = pool
Run Code Online (Sandbox Code Playgroud)
如果您查看 /server/openerp/pooler.py文件,您可以找到get_db_and_pool方法,该方法用于创建并返回数据库连接和新初始化的注册表,还有许多其他与池相关的方法,请查看这些。
def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False):
"""Create and return a database connection and a newly initialized registry."""
registry = RegistryManager.get(db_name, force_demo, status, update_module)
return registry.db, registry
def restart_pool(db_name, force_demo=False, status=None, update_module=False):
"""Delete an existing registry and return a database connection and a newly initialized registry."""
registry = RegistryManager.new(db_name, force_demo, status, update_module)
return registry.db, registry
def get_db(db_name):
"""Return a database connection. The corresponding registry is initialized."""
return get_db_and_pool(db_name)[0]
def get_pool(db_name, force_demo=False, status=None, update_module=False):
"""Return a model registry."""
return get_db_and_pool(db_name, force_demo, status, update_module)[1]
Run Code Online (Sandbox Code Playgroud)
最后调用字典的 get 方法来获取指定键的值,甚至可以使用self.pool['model_name'],字典的任何方法都可以与 pool 一起使用。