python 和 odoo 中的 self.pool.get() 是什么?

What is self.pool.get() in python and odoo?

我假设它用于引用 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}

self.pool.get()用于从注册池中获取orm模型的Singleton实例

如果您想为任何其他模型调用 orm 方法,您可以使用 self.pool.get('model').orm_method

并且在新 API 中,如果您想直接从对象调用 ORM 方法,您可以使用 self.env['obj'].method 而不是 self.method

希望对您有所帮助。

self.pool.get()

这里的pool只是一个类似字典的对象,用于存储OpenERP models的实例,如res.usersir.model.data

OpenERP/Odoo的多数据库特性:单个OpenERP服务器进程可以管理多个数据库,对于每个数据库,已安装模块的集合,因此型号,可能会有所不同。

所以,我们有不同的 pools,每个数据库一个,并且引用我们已经存在的模型实例的正常方法,self

感谢 The Hypered Blogself.pool.get() 的精彩解释。 有关详细信息,请查看 link.

pool is a registry (dictionary object {}).

The registry is essentially a mapping between model names and model instances. There is one registry instance per database.

server/openerp/osv/orm.py

class BaseModel(object):

    def __init__(self, pool, cr):
    """ 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

Refer /openerp/sql_db.py to see how Odoo-Postgresql connection pool establish.

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

连接class

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

如果你查看 /server/openerp/pooler.py 文件,你可以找到 get_db_and_pool 方法用于创建和return一个数据库连接和一个新初始化的注册表,还有很多其他与池相关的方法看这些。

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]

最后调用字典的get方法获取指定key的值,即使可以使用也可以使用self.pool['model_name'] ,字典的任何方法都可以与池一起使用。