Search code examples
odooodoo-10

Show A specific FormView From ir.actions.server


I created Two form Views for res.partner module. The first one is inherited, and the second one Showing only few fields :

<record model="ir.ui.view" id="program_viewform">
            <field name="name">My Program</field>
            <field name="model">res.partner</field>
            <field name="arch" type="xml">
                <form>
                    <separator string="My Program " />
                <field name="projects_ids"  nolabel="True"/>
                    <separator string="submitted Tasks" />
                    <field name="submission_task_ids" nolabel="True"/>

                </form>
            </field>
        </record>

And used this action :

<record model="ir.actions.server" id="myprogram_action">
        <field name="name">My Program</field>
            <field name='model_id' ref='base.model_res_partner'/>
             <field name="state">code</field>
            <field name="code">
                action = {
                    'type': 'ir.actions.act_window',
                    'name': 'My Program',
                    'view_mode': 'form',
                    'view_type': 'form',
                    'view_id': 'ref="training_program_management.program_viewform"',
                    'context': '{ "form_view_ref":"program_viewform"}',
                    'res_model': 'res.partner',
                    'res_id': int(env['res.users'].browse(env.user.partner_id.id)),
                    'views': [(True, 'form')],
                }
            </field>
        </record>

It works Pefectly right, with no errors, The problem is it shows the first ForView, and i need to show the view with the id: program_viewform. I still have a bit confusion in ir.actions.server, and i don't understand the use of this line :

'views': [(True, 'form')],

And why this line doesn't show me the desired View ?

 'view_id': 'ref="training_program_management.program_viewform"', 

Any explanation would be helpful.


Solution

  • don't right the return directly in the server action create a method that return the same dictionary, python code in XML is hard to read and write.

        <field name="code">action = model.open_form_view()</field>
    

    and in your model define this method:

        @api.model
        def open_form_view(self):
            # FIRST GET THE ID OF THE VIEW
            form_id = self.env.ref('training_program_management.program_viewform').id
            # if you want to open the res partener related to the user
            recor_id = self.env.user.partner_id.id
            
            return {
            'type': 'ir.actions.act_window',
                            'name': 'My Program',
                            'view_mode': 'form',
                            'view_type': 'form',
                             # form_id accept integer value.
                             # if you are using only one view 
                             # no need to use views
                            'view_id': form_id,
                            'context': {},
                            'res_model': 'res.partner',
                            'res_id': record_id ,
                            # but if you are using more then one view
                            # you must use views to pass mutliple ids
                            #'views': [(form_id, 'form'), (tree_id, 'tree')],
                            # and to specify the search view use
                            # 'search_view_id': search_view_id,
            }