Lemme first explain what I exactly mean by “Creating Views Dynamically”.
The views in OpenERP is done using .xml file. But there are scenarios where we do not know the exact layout of form or the total number of fields to be displayed. It varies with user’s choice. This is where Dynamic Views come into picture.
The function used to achieve dynamic view is fields_view_get() which can be inherited in any class. The signature of method is as follows-
def fields_view_get(self, cr, uid, view_id=None, view_type=’form’, context=None, toolbar=False, submenu=False)
Assume my_school object which stores details of student as well as professors. Now, while displaying the form we want the appropiate label to be displayed. In its simplest form, the function will be like this-
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False,submenu=False):
result = super(my_school, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar,submenu)
school_obj = self.pool.get('my.school')
active_id = context.get('active_id', False)
label_value = "Student Information" if school_obj.browse(cr,uid,active_id).is_student else "Professor Information"
result['arch'] = '''<form string="Information">
<separator string="%s" colspan="4" />
<group colspan="4" col="2">
<field name="name" />
<field name="account_no" />
<group colspan="4" col="1">
<button special="cancel" string="_Close" icon="gtk-cancel"/>
Now, this function can be used smartly to meet one’s requirement.
view_type attribute can be used to work on form or tree. If we need to work on tree, then-
if view_type == ‘tree’:
//Code goes here
If a portion of view needs to be modified, then we can do something like this-
result['arch'] = result['arch'].replace('<page string="marks_placeholder">', "<page string="Marks"><field name="sub1" /><field name="sub2" /></page>")
<page string=”marks_placeholder”> will be written in original .xml view and then it can be replaced by appropiate fields in .py file.