How to display a hierarchy name for a custom model

THE CHALLENGE:

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

screen-shot-2016-09-20-at-10-01-20-am

THE SOLUTION:

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 product.py. 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' )
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s