odoorpc.models¶
Provide the Model
class which allow to access dynamically to all
methods proposed by a data model.
-
class
odoorpc.models.
Model
¶ Base class for all data model proxies.
Note
All model proxies (based on this class) are generated by an
environment
(see theodoorpc.ODOO.env
property).>>> import odoorpc >>> odoo = odoorpc.ODOO('localhost', port=8069) >>> odoo.login('db_name', 'admin', 'password') >>> User = odoo.env['res.users'] >>> User Model('res.users')
Use this data model proxy to call any method:
>>> User.name_get([1]) # Use any methods from the model class [[1, 'Administrator']]
Get a recordset:
>>> user = User.browse(1) >>> user.name 'Administrator'
And call any method from it, it will be automatically applied on the current record:
>>> user.name_get() # No IDs in parameter, the method is applied on the current recordset [[1, 'Administrator']]
Warning
Excepted the
browse
method, method calls are purely dynamic. As long as you know the signature of the model method targeted, you will be able to use it (see the tutorial).-
__getattr__
(method)¶ Provide a dynamic access to a RPC instance method (which applies on the current recordset).
>>> Partner = odoo.env['res.partner'] >>> Partner.write([1], {'name': 'YourCompany'}) # Class method True >>> partner = Partner.browse(1) >>> partner.write({'name': 'YourCompany'}) # Instance method True
-
__getitem__
(key)¶ If key is an integer or a slice, return the corresponding record selection as a recordset.
-
__iter__
()¶ Return an iterator over self.
-
classmethod
browse
(ids)¶ Browse one or several records (if ids is a list of IDs).
>>> odoo.env['res.partner'].browse(1) Recordset('res.partner', [1])
>>> [partner.name for partner in odoo.env['res.partner'].browse([1, 3])] ['YourCompany', 'Administrator']
A list of data types returned by such record fields are available here.
Returns: a Model
instance (recordset)Raise: odoorpc.error.RPCError
-
id
¶ ID of the record (or the first ID of a recordset).
-
ids
¶ IDs of the recorset.
-
with_context
(*args, **kwargs)¶ Return an instance equivalent to self attached to an environment based on self.env with another context. The context is taken from self.env or from the positional argument if given, and modified by kwargs.
Thus, the following two examples are equivalent:
>>> Product = odoo.env['product.product'] >>> product = Product.browse(1) >>> product.with_context(lang='fr_FR') Recordset('product.product', [1])
>>> context = product.env.context >>> product.with_context(context, lang='fr_FR') Recordset('product.product', [1])
This method is very convenient to update translations:
>>> product_en = Product.browse(1) >>> product_en.env.lang 'en_US' >>> product_en.name = "My product" # Update the english translation >>> product_fr = product_en.with_context(lang='fr_FR') >>> product_fr.env.lang 'fr_FR' >>> product_fr.name = "Mon produit" # Update the french translation
-
with_env
(env)¶ Return an instance equivalent to self attached to env.
-