How to force commit inside a loop?


I found myself creating and updating hundreds of rows of data in a loop. While this was working just fine I wasn’t seeing any of my new or updated data in Odoo until the loop had completed processing all the rows of data. There must be a way to force commit after each loop.

I posted the question on the Odoo forum and a user provided a great solution but it was a bit more complicated in lines of code than what I had hoped for. After a little bit more research I found the answer was very simple.


You need to retrieve the cursor object so you can run the commit() function on it. Simply use the self object’s environment variable to force commit. But be warned this can have dire effects if you aren’t handling your data properly per the Odoo guidelines.

How to display a hierarchy name for a custom model


I needed to display a hierarchy name for a custom model similar to what you see in product categories so I could easily identify the hierarchy relationship of a category at a glance. Notice how the name is actually a hierarchy representation of the category name and its parent(s).



Use Odoo’s model inheritance.

Create a new model (i.e. “new_model”), use Odoo’s model inheritance and inherit product.category which gives the new model the same fields and functions of the model inherited using _inherit. The new model now also includes a field complete_name, a computed field created using the name_get_func found in This computed field is the magic behind displaying the hierarchy name. You can now add complete_name to your view and display the hierarchy name as desired.

Below is the finished working model definition:

class new_model( models.Model ):
    _name = "new.model"
    _inherit = 'product.category'
    _parent_store = True
    parent_left = newFields.Integer( index = True )
    parent_right = newFields.Integer( index = True )
    name = newFields.Char( string = 'Category Name' )
    child_id = newFields.One2many( 'cb.public.catalog.category', 'parent_id', string = 'Child Categories' )