In Odoo, currently, it’s impossible to dynamically change a field's string or label directly from XML. There will be scenarios where we will need to change the string of a field dynamically. For example; If there are two company and should show different strings or labels for a field for each company. In such situations, we can use the _get_view function to dynamically change the field string. The _get_view method will be called when the view of the object is rendered on the screen.
Before Odoo 16, the method utilized for this purpose was 'fields_view_get'. However, in Odoo 16 and Odoo 17, 'fields_view_get' has been renamed to '_get_view'. This change was made because 'fields_view_get' no longer returns the field description, and as a result, it is now referred to as 'get_view'.
Difference between the ‘fields_view_get’ and ‘get_view’
1) The first difference between the ‘fields_view_get’ and ‘get_view’ is their arguments.
In fields_view_get:
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
In _get_view:
def get_view(self, view_id=None, view_type='form', **options):
The arguments `toolbar=False, submenu=False` for the method
‘fields_view_get’ is converted to a kwargs `**options` in `_get_view`.
2) The `fields_view_get` previously returned a dictionary containing the view architecture in the text along with some view information. Now, the `get_view` method returns a tuple consisting of the view architecture as an `etree` node and the view as a browsing record.
Please refer to the blog for more information regarding How to Dynamically Change Label/String using fields_view_get.
https://www.cybrosys.com/blog/how-to-dynamically-change-label-string
Now let’s look at how we can use the _get_view method for dynamically changing the field label or string.
Let’s consider a scenario where we have two companies named “Company A” and “Company B”.
Then we have to change the label of a field called “Source Document” to “Order Reference” if the active company is Company A.
To implement the above-mentioned case, first, we need to inherit the “stock.picking” model since the field “Source Document” is an already existing field in Odoo’s “stock.picking” model. After inheriting the model, let’s call the “_get_view” function.
from odoo import api, models
class StockPicking(models.Model):
_inherit = 'stock.picking'
@api.model
def _get_view(self, view_id=None, view_type='form', **options):
arch, view = super()._get_view(view_id, view_type, **options)
active_company = self.env.company
if view_type == 'form' and active_company.name == 'Company A':
for node in arch.xpath("//field[@name='origin']"):
node.set('string', 'Order Reference')
return arch, view
Since we have to change the field string in Form view and if the active company is Company A case only, a condition if view_type == 'form’ and active_company.name == 'Company A':” is written before setting the string. ‘origin’ is the name of the field “Source Document”.
The below image depicts the result of this _get_view function;
It can be viewed upon reload that the string has been changed to Order Reference in Company A and no change to the string in Company B.
This is how we can simply dynamically change the label/string of the field using _get_view method.