How it works...

Step 1 uses odoo-bin shell to start the Odoo shell. All the usual command-line arguments are available. We use -c to specify a project configuration file and --log-level to reduce the verbosity of the logs. When debugging, you may want to have a logging level of DEBUG for some specific add-ons.

Before providing you with a Python command-line prompt, the odoo-bin shell starts an Odoo instance that does not listen on the network and initializes some global variables, which are mentioned in the output:

  • env is an environment that's connected to the database and specified on the command line or in the configuration file.
  • odoo is the odoo package that's imported for you. You get access to all the Python modules within that package to do what you want.
  • openerp is an alias for the odoo package for backwards-compatibility.
  • self is a recordset of res.users that contains a single record for the Odoo superuser (Administrator), which is linked to the env environment.

Steps 3 and 4 use env to get an empty recordset and find a record by the XML ID. Step 5 calls the method on the product.product recordset. These operations are identical to what you would use inside a method, with the small difference that we use env and not self.env (although we can have both, as they are identical). Take a look at Chapter 6, Basic Server-Side Development, for more information on what is available.

Step 6 commits the database transaction. This is not strictly necessary here because we did not modify any record in the database, but if we had done so and wanted these changes to persist, this is necessary; when you use Odoo through the web interface, each RPC call runs in its own database transaction, and Odoo manages these for you. When running in shell mode, this no longer happens and you have to call env.cr.commit() or env.cr.rollback() yourself. Otherwise, when you exit the shell, any transaction in progress is automatically rolled back. When testing, this is fine, but if you use the shell, for example, to script the configuration of an instance, don't forget to commit your work!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset