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