+3 votes
412 views
what is the use of field_view_get ?
in Server by (10.5k points) | 412 views

2 Answers

+4 votes
Best answer
  • Sometimes, its needed to add or remove or edit some properties of fields in dynamic mode. Normally, we can manage view from xml.

  • So, in odoo  there is fields_view_get() method in which we can divert view or do some operation.

  • Another thing some times its needed to redirect depending on state or else.

by (5.3k points)
selected by
can you give an exemple for each use please
+4 votes

Creating dynamical view in OpenERP


Normally, the views are written in .xml file. At times, we are not familiar with the exact view of form, the total number of fields to be displayed or properties of fields. It may be dependent on user’s choice. This is where Dynamic Views comes into picture.

The function used to achieve dynamic view is fields_view_get() which can be inherited in any class.


Create dynamic view in OpenERP

Sometimes, its needed to add or remove or edit some properties of fields in dynamic mode. Normally, we can manage view from xml.

So, in OpenERP there is fields_view_get() method in which we can divert view or do some operation.

Another thing some times its needed to redirect depending on state or else.

Example : 

Case 1 : We have two form view parent and child, now from the tree view on the basis of state we have to call either parent or child.
        
          def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):       
              if view_type == 'form':
                  if state == 'A' :
                      view_id = self.pool.get('ir.ui.view').search(cr,uid,[('name', '=', 'parent.form')])
                  elif state == 'B':
                      view_id = self.pool.get('ir.ui.view').search(cr,uid,[('name', '=', 'child.form')])
              res = super(my_module,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)


Case 2 : We have to change some fields using fields_view_get()

     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
        if context is None:
            context = {}
        res = super(account_invoice_line,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
        if context.get('type', False):
            doc = etree.XML(res['arch'])
            for node in doc.xpath("//field[@name='product_id']"):
                if context['type'] in ('in_invoice', 'in_refund'):
                    node.set('domain', "[('purchase_ok', '=', True)]")
                else:
                    node.set('domain', "[('sale_ok', '=', True)]")
            res['arch'] = etree.tostring(doc)
        return res

by (5k points)
205 questions
180 answers
37 comments
222,944 users