Search code examples
xmlpython-2.7viewodooopenerp-7

OpenERP. Open from tree a form but from other related field


I'm building a custom view for event.event with a tree for registration_ids field. Normally, when I clicked a tree element the event.regitration model form is opened in a popup.

So, for my objective I want to change this behaviour opening in this case the res.partner related with the event.registration by partner_id field.

This is that I have:

       <record id="tutorship_course_form" model="ir.ui.view">
            <field name="name">tutorship.course.form</field>
            <field name="model">event.event</field>
            <field name="arch" type="xml">
                <form string="Curso" version="7.0">
                    <sheet>
                        <label for="event_type" string="Curso"/>
                        <field name="event_type" readonly="1" />
                        <label for="date_begin" string="Fecha"/>
                        <field name="date_begin" readonly="1" />
                        <label for="city" string="Ciudad"/>
                        <field name="city" readonly="1" />
                        <field name="registration_ids" colspan="3">
                            <tree>
                                <field name="firstname" />
                                <field name="lastname" />
                                <field name="email" />
                            </tree>
                        </field>
                    </sheet>
                </form>
            </field>
        </record>        

        <record id="tutorship_courses_tree" model="ir.ui.view">
            <field name="name">tutorship.courses.tree</field>
            <field name="model">event.event</field>
            <field name="arch" type="xml">
                <tree string="Cursos">
                    <field name="event_type" string="Curso" />
                    <field name="date_begin" string="Fecha" />
                    <field name="city" string="Ciudad" />
                </tree>
            </field>
        </record>

        <record model="ir.actions.act_window" id="action_courses_view">
           <field name="name">Cursos</field>
           <field name="type">ir.actions.act_window</field>
           <field name="res_model">event.event</field>
           <field name="view_mode">tree,form</field>
           <field name="domain">[('visible','=',1)]</field>
           <field name="view_id" ref="tutorship_courses_tree" />
        </record>

        <record id="action_for_courses_tree_view" model="ir.actions.act_window.view">
            <field name="sequence" eval="0" />
            <field name="view_mode">tree</field>
            <field name="view_id" ref="tutorship_courses_tree"/>
            <field name="act_window_id" ref="action_courses_view"/>
        </record>
        <record id="action_for_courses_form_view" model="ir.actions.act_window.view">
            <field name="sequence"  eval="1" />
            <field name="view_mode">form</field>
            <field name="view_id" ref="tutorship_course_form"/>
            <field name="act_window_id" ref="action_courses_view"/>
        </record>

I haven't got clear how to ge it. Is this possible?

Thank you in advance


Solution

  • You can't do it working with ORM methods and XML. I guess you would have to use JavaScript in order to achieve that.

    What I would do in your case is to add a button in each line of the tree (with only an icon, or with the string Open partner, if you prefer) which calls the respective partner form:

    XML code (inside your tree)

    <button name="open_partner_form" type="object" icon="icon_you_want" help="Open partner info"/>
    

    Python code (inside event.registration model)

    @api.multi
    def open_partner_form(self):
        form_view_id = self.env.ref(
            'base.view_partner_form').id
        for event_registration in self:
            return {
                'name': _('%s') % event_registration.partner_id,
                'view_type': 'form',
                'view_mode': 'form',
                'res_id': event_registration.partner_id.id,
                'views': [(form_view_id, 'form'), ],
                'res_model': 'res.partner',
                'type': 'ir.actions.act_window',
                'target': 'current',
                'flags': {'action_buttons': True},
            }
    

    If it helps you, if you add (in XML view) context="{'form_view_ref': 'module_name.xml_form_view_id'}" to the one2many field (registration_ids), you will be able to select the form to be opened... but this form must belong to the same model of registration_ids comodel (I think it was event.registration).