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..

3 comments:

  1. I've created one field and I want to add that field in view using fields_view_get then how can I do that?

    ReplyDelete
  2. Hello !!! Pinakin Nayi . In the above post how will u retrieve the value of state bcoz right now if i directly use ur code then it is throwing me an error saing that global variable "state" is not recongnized.So first we have to retrieve the value of state from the form and then compare it.But how do we do that ...Please can u help me out.My main objective is to make the record readonly uncer certain situations and for that i need to retrieve the value of a particular field from the form view and then modify the view accordingly.But how to retrieve that ....i have no clue about it......Your help will be deeply appreciated.Thanx in advance

    ReplyDelete