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’]”):
for node in doc.xpath(“//field[@name=’partner_id’]”):
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.