Fields_view_get in Odoo

In Odoo,views are implemented with the help of xml files. Each xml file always consist of form views,list views,kanban views and etc. Every view has its own structure where we usually place input fields from users.Many times, we are not sure with the exact view of form, also number of fields to be presented or their properties. It varies as per User’s choice. This is place where we need to load views dynamically.The method used to load dynamic view is fields_view_get().So, in Odoo there is fields_view_get() method in which we can load views as per user’s preferrences.Odoo matches this requirement through fields_view_get method to great extent.

The syntax of method is as follows:

def fields_view_get(self, cr, uid, view_id=None, view_type=’form’, context=None, toolbar=False, submenu=False)

Lets consider Accounting module.If we have a look at Customer Invoices and Supplier Invoices tree view, it is the same view belonging to both the menu’s.But in Customer Invoices, the string for field ‘partner_id’ is Customer and for that of Supplier Invoices,it is Supplier. It is basically implemented with the help of fields_view_get method to load views dynamically.

The following code snippet explains how it is implemented with the help of above method.

if view_type == ‘tree’:
partner_string = _(‘Customer’)
if context.get(‘type’) in (‘in_invoice’, ‘in_refund’):
partner_string = _(‘Supplier’)
for node in doc.xpath(“//field[@name=’reference’]”):
node.set(‘invisible’, ‘0’)
for node in doc.xpath(“//field[@name=’partner_id’]”):
node.set(‘string’, partner_string)

So it is clear from above piece of code that if view is tree view and type of invoice is either Supplier Invoices or Supplier Refunds,then the string of field ‘partner_id’ is dynamically changes to Supplier and thats the reason we can see it in Supplier Invoices/Supplier Refunds tree view. Many examples can be found across various odoo modules.

Thus users can implement fields_view_get method as per their preferrences and it will present users with the required views along with necessary properties.

2 responses to “Fields_view_get in Odoo”

  1. George Vincent says:

    Dear Team,
    I would like to change field label while onchange many2one field. I could not use fields_view_get because it works while open a form. Is there any other way to achieve it please help me i am struck.

    Thank you

You must be logged in to post a comment.