Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Dynamic view in on_change function

on 3/2/15, 2:35 AM 926 views

Hi all,

How can I manage dynamic view by on_change function????

I have this view of a model:

<!-- Begening View dynamic.fields -->

<record id='dynamic_fields_tree' model='ir.ui.view'>

            <field name='name'>dynamic.fields.tree</field>
            <field name='model'>dynamic.fields</field>
            <field name='arch' type='xml'>
                <tree string='Dynamic Fields'>                    
                    <field name='name'/>
                    <field name='type_id'/>
       <record id='dynamic_fields_form' model='ir.ui.view'>
            <field name='name'>dynamic.fields.form</field>
            <field name='model'>dynamic.fields</field>
            <field name='arch' type='xml'>
                <form string='Dynamic Fields' version="7.0">
                            <field name='date'/>
                            <field name='type_id' on_change='onchange_type_id(type_id)'/>
                            <field name='name' invisible='1'/>
                            <page string='Dynamic'>
                                <field name='answered' invisible='1'/>
                                <field name='lastname' invisible='1'/>
                                <field name='city' invisible='1'/>
                                <field name='married' invisible='1'/>
       <record id='dynamic_fields-act' model='ir.actions.act_window'>
            <field name='name'>Dynamic Fields</field>
            <field name='res_model'>dynamic.fields</field>
            <field name='view_type'>form</field>
            <field name='view_id' ref='dynamic_fields_tree'/>
            <field name='help' type='html'>
              <p class='oe_view_nocontent_create'>
                Create Dynamic Fields. 
            name='Dynamic Fields' 

<!-- End view dynamic.fields -->

And in the function "onchange_type_id", I have:

def onchange_type_id(self, cr, uid, ids, type_id, context=None):
        if context is None:
            context = {}
        res = super(test_dynamic_fileds,self).fields_view_get(cr, uid, view_id=None, view_type='form', context=context, toolbar=False, submenu=False)
        all_fields = []
        dyn_op_fields_obj = self.pool.get('dynamic.operation.champs')
        type_op_fields = dyn_op_fields_obj.search(cr, uid, [('operation_id', '=', type_id)])
        for t in dyn_op_fields_obj.browse(cr, uid, type_op_fields):
        arch = etree.XML(res['arch'])
        for f in all_fields:
            for node in arch.xpath("//field[@name='" + f + "']"):
                node.set('invisible', '0')
        res['arch'] = etree.tostring(arch)

return res

I want for a given type, the view is different.
Ex: for the type 1, the field name, city, married are visible and not visible to the type 2.



Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.



Tarek Mohamed Ibrahim
On 5/20/15, 4:26 PM

Hi  Bernscheinder,

I think you can achieve your goal in the example by using the attrs tag as follows

 <page string='Dynamic'>
<field name='married' attrs="{'invisible':['type','=',2)]}"/>

On 5/29/15, 4:21 AM

Thx, i've resolved it!!

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 3/2/15, 2:35 AM
Seen: 926 times
Last updated: 6/14/15, 7:56 AM