OdooGap Blog / Undocumented and less known coding tricks

Undocumented and less known coding tricks


Some Odoo development features and tricks

On the last 6/7 years Odoo framework has had accelerated growth and though there is a big effort to improve the documentation it's still to cover the complete framework.

Sometimes these are documented in the code in some comment lines. Other times you just find them being used and I usually collect them.

Create a calculated Selection field


Most times a list of values is quite enough, some other time you need something the is between creating a full new object or just filtering out of a dictionary.

The following example it's just an example that could also be implemented with a many2one and a selection widget as well:

@api.model
def _default_billing_code(self):
    """ This routine will generate the selection"""
    sel = self.env['lookup.codes'].sudo().search([('type', '=', 'client')])
    return [(s.code, s.name) for s in sel]

billing_code = fields.Selection('_default_billing_code', string='Billing Code')


Show Different View On a Widget


When you have a many2many or a many2 one widget and you need to show a different search, list and widget

<field name="contact_ids" context="{
        'form_view_ref':'base.contact_form',
        'search_view_ref':'base.contact_search',
        'tree_view_ref':'base.contact_tree'}"/>


Action Window attributes and flags


Window actions can be used both with xml or directly with python. I found they really have several undocumented options and these are just the ones i was able to list. I know others and i will add them to this page as soon as I get to understand the effect they have on the views.

        def some_button_method(self):

        ...

        return {
            # These are the usual options you will know for sure
            'type': 'ir.actions.act_window',
            'name': _('Chart of Accounts'),
            'res_model': 'account.account',
            'view_mode': 'tree',
            'search_view_id': self.env.ref('account.view_account_search').id,
            'domain': domain,
            ....
            # Default limit for the list view (default is 80). be careful as using higher values will make page load slower
            'limit': 500,
            # Used to filter menu and home actions
            'usage': 'menu',
            # These flags can be used for other things, these are the ones i found
            'flags': {
                # Show or not the create and Save buttons
                'sidebar': False,
                # Action drop down
                'action_buttons': True,
            }
            # This will allow us to define the order and specific id of the view types we want to show
            'views': [
                         [view_id, 'list'],
                         [form_id, 'form'],
                         [search_id, 'search']
                     ],
        }


We can consider this an ongoing article as I intend to collect and list all the ones I thing as i go along. Please feel to contact me if you want to add something to this list dduarte (at) odoogap.com.