# 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 the `odoorpc.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([2])  # Use any methods from the model class
      [[1, 'Mitchell Admin']]

  Get a recordset:

      >>> user = User.browse(2)
      >>> user.name
      'Mitchell Admin'

  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, 'Mitchell Admin']]

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

  __eq__(other)

    Return self==value.

  __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.

  __init__()
  __iter__()

    Return an iterator over self.

  __ne__(other)

    Return self!=value.

  __repr__()

    Return repr(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', 'Mitchell Admin']

    A list of data types returned by such record fields are available here.

    Returns

      a `Model` instance (recordset)

    Raise

      `odoorpc.error.RPCError`

  property id

    ID of the record (or the first ID of a recordset).

  property ids

    IDs of the recorset.

  classmethod with_context(*args, **kwargs)

    Return a model (or recordset) equivalent to the current model (or recordset) attached to an environment with another context. The context is taken from the current environment or from the positional arguments args if given, and modified by kwargs.

    Thus, the following two examples are equivalent:

        >>> Product = odoo.env['product.product']
        >>> Product.with_context(lang='fr_FR')
        Model('product.product')

        >>> context = Product.env.context
        >>> Product.with_context(context, lang='fr_FR')
        Model('product.product')

    This method is very convenient for example to search records whatever their active status are (active/inactive):

        >>> all_product_ids = Product.with_context(active_test=False).search([])

    Or to update translations of a recordset:

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

  classmethod with_env(env)

    Return a model (or recordset) equivalent to the current model (or recordset) attached to env.