ERPGAP Blog / Undocumented and less known coding tricks
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']
                     ],
        }

How to send a direct message from code


Some occasions might be useful to send an instant message to some user when some process is finished or when some other event has been completed.

This code is tested for 10.0 and 13.0 from the Odoo shell, so i guess it will work also for 11.0 and 12.0:

In [1]: partner_id = env['res.users'].browse(36).partner_id.id
In [2]: cn = env['mail.channel'].channel_get([partner_id])
In [3]: env['mail.channel'].browse(cn['id']).message_post(body="<p>Hello from console</p>", message_type="comment", subtype="mail.mt_comment")
Out[1]: mail.message(2067404,)
In [4]: env.cr.commit()

We can consider this an ongoing article as I intend to collect and list all the ones I thing as i go along.

Contact Us


Other articles



How to Setup an Odoo development environment

How to Setup an Odoo development environment

About us

Odoo is a management software that fits all sizes. This software is for small, medium and large companies. Your company's information is all in one place. You don't have to go back and forth to check something. All apps have real-time access to your database.

Tags tags