Fields view get  in Odoo

Fields view get in Odoo In Odoo, views are implemented with the help of XML files. Each XML file always consists 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 a number of fields to be presented or their properties. It varies as per User’s choice. This is a 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 preferences.Odoo matches this requirement through fields_view_get method to a great extent.

The syntax of the method is as follows:

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

Let us 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.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 the view is tree view and type of invoice is either Supplier Invoices or Supplier Refunds, then the string of field ‘partner_id’ is dynamically changed to Supplier and that 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 preferences and it will present users with the required views along with necessary properties.

    2 replies to "Fields view get in Odoo"

    • George Vincent

      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

      • nevpro

        Hi George,

        Thanks for contacting us. Yes,certainly there is a way to implement this scenario in Odoo. Kindly furnish more details about your query.

Leave a Reply