3

I assume it is used to refer fields in other modules in openerp, but I am not sure.

Here they are somehow getting price from one products module to sales module.

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}
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
Kiran
  • 1,481
  • 6
  • 36
  • 66

3 Answers3

5

self.pool.get()

The pool is here just a dictionary-like object that is used to store instances of the OpenERP models, like res.users or ir.model.data.

Multi-database nature of OpenERP / Odoo: a single OpenERP server process can manage multiple databases, and for each database, the set of installed modules, and thus the set of models, can vary.

So, we have different pools, one per database, and the normal way to reference the model instance we are already in, self

Thanks to The Hypered Blog for the great explanation on the self.pool.get(). For more info check that link.

bud-e
  • 1,511
  • 1
  • 20
  • 31
5

Pool

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

Connection 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

if you look at the /server/openerp/pooler.py file there you can find the method get_db_and_pool which is used to Create and return a database connection and a newly initialized registry, there are many other methods related to pool look at these.

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]

And finally you call get method of dictionary to get the value of the specified key, even you can use can use self.pool['model_name'], any method of dictionary can be used with pool.

3

self.pool.get() is used to get the Singleton instance of the orm model from the registry pool.

If you want to call orm methods for any other model you can use self.pool.get('model').orm_method

And in new API, if you want to call ORM method directly from an object you can use self.env['obj'].method instead of self.method

Hope this helps.

Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
Baiju KS
  • 659
  • 8
  • 12