Odoo Help

Welcome!

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.

0

Dynamic view in on_change function

By
bernscheinder
on 3/2/15, 2:35 AM 997 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'/>
                </tree>
            </field>
       </record>       
       <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">
                        <group>                   
                            <field name='date'/>
                            <field name='type_id' on_change='onchange_type_id(type_id)'/>
                            <field name='name' invisible='1'/>
                        </group>
                        <notebook>
                            <page string='Dynamic'>
                                <field name='answered' invisible='1'/>
                                <field name='lastname' invisible='1'/>
                                <field name='city' invisible='1'/>
                                <field name='married' invisible='1'/>
                            </page>
                        </notebook>
                </form>
            </field>
       </record>       
       <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. 
              </p>
            </field>
        </record>        
        <menuitem 
            action='dynamic_fields-act' 
            name='Dynamic Fields' 
            id='menu_dynamic_fields-act' 
            parent='base.menu_sales'
            sequence='40'/>

<!-- 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):
            all_fields.append(t.name)
        
        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.

Thx,

0

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
896
| 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.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

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)]}"/>
</page>


0
bernscheinder
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)

Stats

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