Enable Dark Mode!
how-to-dynamically-change-the-string-of-a-field-using-get-view-in-odoo-17.jpg
By: Aiswarya JP

How to Dynamically Change the String of a Field Using _get_view in Odoo 17

Technical Odoo 17 Odoo Enterprises Odoo Community

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.

how-to-dynamically-change-the-string-of-a-field-using-get-view-in-odoo17-1-cybrosys

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;

how-to-dynamically-change-the-string-of-a-field-using-get-view-in-odoo17-2-cybrosys

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.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message