Monday, 26 August 2013

Creating Views Dynamically 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. 




Syntax:
def fields_view_get(self, cr, uid, view_id=None, view_type=’form’, context=None, toolbar=False, submenu=False)
If we only want to alter propeties of fields,then  fields_view_get also provide scope for that.
You can change “onchange”  function or readonly ,required property of a particular field.
Example-
Overriding predefined function fields_view_get ,which also calls predefined function for the class “reception” in the line “result = super(reception, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)”.
 Generating fields for the 'Form' view on the basis of 'User' clicks Buttons.
            If 'User' clicks 'receive' Button then 3 fields with the name user_id ,vehicle_no and receiving lines are added.
            If 'User' clicks 'cancel' Button then 2 fields with the name user_id  and receiving lines  are added . Here Form name also printed according to the 'Button' clicked.
      
Process-

      1.  First add fields
                        ex-_moves_fields = result['fields']
                            _moves_fields.update({ "user_id" : {'string': 'Checked By','type': 'many2one','relation':'res.users','readonly':True},})
  1. Now add view, accordingly you want .You can add groups ,separators and apply properties like- 'readonly','required' or colspan etc..
ex-_moves_arch_lst = """<form string=" "%s" Products">
                               """% (context.get('button'))
_moves_arch_lst += """<field name="user_id"    />  ''''''
  1. Now update fields and views into returning dictionary  and return dictionary
ex- result['fields'] = _moves_fields
      result['arch'] = _moves_arch_lst
return result
1.       
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):       
        result = super(reception, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
        incm_id = context.get('active_ids', False)
            if view_type=='form':
                _moves_fields = result['fields']
            if context.get('button') == 'receive':
                        _moves_arch_lst = """<form string=" "%s" Products">
                               """% (context.get('button'))
                        _moves_fields.update({
                        "user_id" : {'string': 'Checked By','type': 'many2one','relation':'res.users','readonly':True},
                        "vehicle_no" : {'string': 'Vehicle No','type': 'char','required':True},
                                    "receiving lines" : {'string': 'Receiving Moves','type': 'one2many','relation':'receiving.point'},                                   
                                    })
                        _moves_arch_lst += """<field name="user_id"    />
                                      <field name="vehicle_no"/>
                                                      <field name="receiving lines" colspan="6" nolabel="1">
                                                            <button name="cancel receiving" string="Cancel Quantity" colspan="1" icon="gtk-apply" type="object"/> """
               if context.get('button') == 'cancel':
                        _moves_arch_lst = """<form string=" "%s" Products">
                               """% (context.get('button'))
                _moves_fields.update({"user_id" : {'string': 'Checked By','type': 'many2one','relation':'res.users','readonly':True},
                                    "receiving lines" : {'string': 'Receiving Moves','type': 'one2many','relation':'receiving.point'},                                   
                                    })
                        _moves_arch_lst += """<field name="receiving_lines" colspan="6" nolabel="1">
                                                            <button name="receiving" string="Recieve Products" colspan="1" icon="gtk-go-forward" type="object"/> """
                _moves_arch_lst += """ </form>"""
             result['fields'] = _moves_fields
             result['arch'] = _moves_arch_lst
         return result

This wizard opens in case , when we click “Cancel” Button--





This wizard gets open in case,when we click “Receive” Button ---
Note -In the above example we have added lesser field ,because of length of code, any no of field can be added and on the same time we can generate view for the added fields 





Thanks
Sonu Chaudhary

No comments:

Post a Comment