Logging in Odoo

Hi guys,

In this tutorial I will learn you how to log in Odoo. Logging can be very handy to see what actually happens when you do something, to show warnings in the terminal or to notify for extreme problems in the logfile.

1. Importing logging

The first thing that you need to do is to import the logger in your python file. This library is not something from Odoo but this is simply a python library for logging that works in Odoo. Add the import statement at the top of your python file and then call the logger.

2. Using logging to print/log statements

After importing logging you can directly start writing log statements. There are five different states for logging pre-built:

  • logger.debug: for debugging purposes.
  • logger.info: for showing information.
  • logger.warning: for showing minor issues.
  • logger.error: for showing that an operation has failed
  • logger.critical: for showing that something has gone horribly wrong. In Odoo these will be used to notify a module doesn’t work anymore.

You can use logging anywhere in Python and at any time as you wish. In this tutorial I will create a simple field and button. When the  button is printed I will print all kind of pre-built logging states to the terminal for demo purposes. I’ve created a button that calls the function print_log_data:

As you can see logging is very simple in Odoo. Choose what kind of logging it is and then simply add a text to it. if you want to use variables from your view you can simply call them in Python and add them to the message. For example:

When you click on the button the logging statements will be printed in your terminal. For example with my function:
Logging output terminal

Thats it! It is as simple as that to log data in Odoo. If you want to go further though, there are quite some extra options.

3. Advanced logging configuration

Odoo goes a bit further than this though! You can configure multiple logging options in Odoo:

  • logfile: the log filename. If this is not filled in all the log details will be written to stdout (terminal).
  • log_level: any value in the list of [‘debug_rpc_answer’, ‘debug_rpc’, ‘debug’, ‘debug_sql’, ‘info’, ‘warn’, ‘error’, ‘critical’]. Odoo changed the log_level meaning here because these level values are mapped to a set of predefined ‘module:log_level’ pairs.
  • log_handler: can be a list of ‘module:log_level’ pairs. The default value is ‘:INFO’ — it means the default logging level is ‘INFO’ for all modules.
  • logrotate: True/False. If True, create a daily log file and keep 30 files.
  • log_db: True/False. If this is set to ‘True’ the logging will also be written to the ‘ir_logging’ table in the database.

For example if I define a logfile in the terminal:
Define logfile location logger
When you now go to this location you will see there is a new logfile created and that it is automatically filled with the data that you defined:
New logfile

Do you want to see the source code or try this module for yourself? You can download / view it on Github.
Has this tutorial helped you, do you have any feedback or questions? Post away!




Odoo Experts

Creating many2many drag and drop (handle) widget in Odoo

Hi guys,

In this tutorial I will learn you how to create a handle widget. A handle widget gives you the option to re-order lines in a many2many, making records drag and droppable. This is a very hand feature to order items and eventually change the order on reports for example.
An example from before re-arranging a many2many and after:
Re-order handle many2many

1. Creating the model and fields

For a drag and drop (handle) widget you will need a many2many and for having a many2many you need to create a link to another table. The first step is to create a new table, in case you don’t already have one, or otherwise you can skip to part 2. The table where you want to link your many2many to needs atleast one field (such as name) and what you absolutely need is a field named ‘sequence’. The sequence field is an integer and will keep track of the order on your many2many. For example:

As you can see I created a field named sequence which is an integer. The second thing that you need for this field is to give it a default value, otherwise it will not work correctly. After you’ve created your model and fields its time to create your many2many.

2. Creating the many2many (widget)

The second step is to create a many2many which links from the table where you want to use the many2many to the table where you want to link it with. For example:

As you can see I’ve inherited the model sale.order since I want to create my many2many in this model. I then created a new field and created a link to the table sale.order.handle. I’ve also created a default which links to a function. The idea behind this is to automatically fill the many2many handle with all the data from the model sale.order.handle. Ofcourse you don’t have to, this is just for the demo.
Tip: Out of safety uses you should never create a table longer than 16 characters in a many2many! The postgreSQL database cannot handle a many2many link longer than 64 characters in total. For more information see this bug report.

3. Creating the view

Now that you’ve created both your model and the many2many you only need to add the many2many to your view. The code will look like this:

There isn’t to much difference with a normal many2many, there are just three things you should always do.
1. Create your own tree to render the many2many correctly.
2. always add the field ‘sequence’ in this tree.
3. call the widget with widget=”handle”. This will make the many2many drag and droppable.
And thats it, you’ve created your own many2many handle which makes items drag and droppable. The result will look something like this:
handle widget result

Do you want to see the source code or try this module for yourself? You can download / view it on Github.
Has this tutorial helped you, do you have any feedback or questions? Post away!




Odoo Experts

Automatically fill Many2many in Odoo

Hi guys,

In this tutorial I will learn you how to automatically fill a Many2many with data. In some cases a many2many always has to have data so in that case having it filled in automatically is really handy.

1. Creating the model

The first step is to create a new model with fields from which we want to get data. (in case you already have a model where you want to load from skip to chapter 2)
In my example I want to create a many2many which gets all records from the model sale.order.printorder and automatically adds them, so lets first create the model sale.order.printorder:

2. Create and fill many2many

After the model is made we now have to fill up the many2many. Create a many2many to your other model and add the default parameter to it:

As you can see there is a default=_get_default_print_order_ids which links to a function in the class. This function will automatically get all data from the model sale.order.printorder thanks to self.pool. After the pool got all records we will return all the data and it will be added to the many2many.
Now lets create a view that shows the many2many:

When a user would now create a new quotation/order there will be a new tab named ‘Many2many demo’ which is automatically filled with all records from the model sale.order.printorder:
Automatically added records to many2many

3. Creating default records for sale.order.printorder

Want to go further and want to have the records generated by default when the module is installed? Then create a new XML file where you create new records. I’ve created a file defaultdata.xml in my module and added the following records:

TIP: Add the filename defaultdata.xml to __openerp__py under ‘data’.
When the module is installed these records will be automatically created in the database and will never be updated thanks to the noupdate=”1″. When you need a many2many filled with default data that always has to be the same this is perfect. If this is not the case simply create a new view where users can create new records in to the model sale.order.printorder!

Do you want to see the source code or try this module out? You can download / view it on Github!
Has this tutorial helped you, do you have any feedback or questions? Post away!




Odoo Experts

Installing default data in Odoo module

Hi guys,

In this tutorial I will learn you how to install data in a module by default. This will mean that data is automatically inserted in the database whenever you would install this module. In some cases this is very handy because you need some default values inserted when the module is installed.

1. Adding a new XML file to the module

The first thing you need to do is create a new XML file in your custom module, for example demodata.xml, and to add the filename to __openerp__.py. An example of my __openerp__.py file:

Create default data

After you’ve added the location to the file (to __openerp__.py) you should create the data for the file. Open up your custom module, go in to the folder data and edit your XML file. The structure is always the same. You give the record a name (this can be anything, make sure its unique) and add the name of the model where you want data installed in. Then add a field for every field you have in the model which you want to fill with data. The following is an example that creates a record in the model ‘demo.default.data’ when you install the module:

As you can see by this record with demo data it will create a new record with a name named ‘Record #1’ and a description with the text ‘A description for record #1’. These are the fields that I’ve created in my custom model (models.py) like this:

When you would now install this module it will automatically install the records that are defined.
Example demo data

Want to see an example module where you can see how everything works? You can download/view it on Github!
Has this tutorial helped you, do you have any feedback or questions? Post away!




Odoo Experts