Update a Field’s String Using ‘_get_view’ in Odoo 17

FARHA
June 10, 2024
get-view-field

There may be situations when the name or string of a field needs to be displayed dynamically for different users or companies. However, changing a field’s string dynamically based on various conditions using XML can be difficult or also impossible in certain cases. In such situations, the desired outcome can be achieved by using Odoo’s ‘_get_view’ function. This method will be called while rendering the view of the object on the user interface.

Before Odoo 16, the ‘fields_view_get’ function was used for this purpose.

The main difference between the ‘_get_view’  and  ‘fields_view_get’ are –

  1. Function Arguments
  2. fields_view_get
    1. def fields_view_get(self, view_id=None, view_type=’form’, toolbar=False, submenu=False):
  • _get_viewdef get_view(self, view_id=None, view_type=’form’, **options):
  • Function Return
    • fields_view_get – returned a dictionary containing the text’s view architecture and some view information.
    • _get_view – returns a tuple consisting of the view architecture as an `entree` node and the view as a browsing record.

In this blog, we can discuss how to use the ‘_get_view’ function to dynamically change the string of a field based on two different companies.

Let us consider the companies as ‘Company A’ and ‘Company B’.

The below image shows the delivery receipt of a sale order.

Let us consider the field whose string is to be changed dynamically as the  ‘Source Document’.

If the active company is ‘Company A’ then we have to change the string of the field ‘Source Document’ to ‘Order Reference’.

To achieve this, the first step would be to inherit the ‘stock. picking’ model and then calling the ‘_get_view’ function.

In the custom module, create a .py file and inherit the ‘stock. picking’ model as shown below.

In this case, the field name of ‘Source Document’ is ‘origin’. As we have to change the field string/label in form view in the case of ‘Company A’, we have added the condition –

          if view_type == ‘form’ and active_company.name == ‘Company A’

The below image shows the result of the above ‘ _get_view ‘function.

get-view-field-in-odoo

In the above image, it can be seen that in the case of ‘Company A’, the string of the field is displayed as ‘Order Reference’ while in the case of ‘Company B’, the field string is not changed and is displayed as ‘Source Document’.

In this way, we can easily change the field string as well as other field attributes like read-only, invisible etc. dynamically based on certain conditions using the ‘__get_view’ function.

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *