Showing posts with label load different view openerp. Show all posts
Showing posts with label load different view openerp. Show all posts

Tuesday 10 September 2013

Dynamically view in OpenERP



Creating Views Dynamically in OpenERP


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



..Enjoy..