Changing permissions for menus on the fly

If you need to change permissions for the menus on the fly, take into consideration that you would need to reset the cache after that action:

self.env['ir.ui.menu'].clear_caches()

Otherwise, you will not get the result that you want.

Share the knowledge!

How to show the total of a numeric column in a tree view

If you need to show the total of a numeric column in a tree view you would need to use the attribute “sum“. An example is shown below:

<field name="amount" sum="Total Amount"/>

The string that is assigned to this attribute is actually the text that is shown on hover of the value.

Share the knowledge!

Showing a field only in edit mode

If you need to show a field only in edit mode you would need to use the class oe_edit_only. In the example below the field “content“, will be shown only when the form view is in edit mode

<field name="content" widget="html" class="oe_edit_only" />
Share the knowledge!

How to add a new commercial field

Commercial fields are fields that are managed by the commercial entity to which a partner belongs. For example “vat” field is set in the parent company object and its value is transferred to all childs` contacts.

In case if you need to add a new field with the same functionality, then you would need to add the field in the res.partner model and to override the method _commercial_fields.

For example, let`s add the “credit_limit” field as a commercial field:

class ResPartner(models.Model):
    _inherit = "res.partner"

    credit_limit = fields.Float(string='Credit Limit')


    @api.model
    def _commercial_fields(self):
        return super(ResPartner, self)._commercial_fields() + ['credit_limit']
Share the knowledge!

“Delivery Address” field in the account.invoice model

Starting from Odoo 10 you can find the “Delivery Address” field in the account.invoice model as well, in order to see this field you would need to have the group “Addresses in Sales Orders“.

Share the knowledge!

How not to show the icons from the “contact” widget in QWeb templates

If you use the “contact” widget for displaying an address in the QWeb templates and you want the address to be without icons, you would need to set the flag “no_marker” to “True”.

For example, the code for the shipping address in the sale report without icons would look like:

<div t-field="doc.partner_shipping_id" 
    t-options='{"widget": "contact", "fields": ["address", "name", "phone", "fax"],
    "no_marker": True}'/>
Share the knowledge!

Readonly field in onchange method

If you change the value of a readonly field in onchange method, on save it will not be stored in the database. In order to do that you would need to override create/write methods where you would need to update the values dictionary with this value before creation.

For example, you would need to update the readonly field “invoice_method” the depends on the “partner_id” field. If you change its value in the onchange_partner_id method

@api.onchange('partner_id')
def onchange_partner_id(self):
    res = super(MrpRepair, self).onchange_partner_id()
    self.invoice_method = self.partner_id.prepaid and 'b4repair' or 'after_repair'
    return res

the new value will not be stored in the database on save. Instead, you would need to extend the create method and to update the “vals” dictionary before creation

@api.model
def create(self, vals):
    if vals.get('partner_id'):
        vals.update({'invoice_method': self.env['res.partner'].browse(
                    vals.get('partner_id')).prepaid and 'b4repair' or 'after_repair'})
    return super(MrpRepair, self).create(vals)

 

Share the knowledge!

How to set a default tax on product creation.

Odoo allows you to simplify the process of product creation by setting the default Sale and Purchase Tax. In that way, you don’t need to set it always when you create a new product. That can be done in Accounting >> Configuration >> Settings >> Taxes.

Share the knowledge!

Overriding method decorated with “http.route” decorator

If you need to override a method decorated with “http.route”, where all of the arguments in the decorator remind same as on the original method no need to redefine them again, it is enough only to add the decorator “http.route()” on the overridden method.

Share the knowledge!

Set the parameter “report.url” for loading CSS into reports

If the CSS is not loaded into reports and the domain used in the browser can’t be used for generating reports, you would need to set the parameter “report.url” in Settings >> Parameters >> System Parameters to your local address, in my case it was http://127.0.0.1:8069 .

Share the knowledge!