Skip to Content
Menu
This question has been flagged
3 Replies
9272 Views

I found answers to this question but they are outdated: https://www.odoo.com/forum/help-1/question/how-to-open-one2many-form-view-of-the-correspondent-line-in-tree-view-112388

I have 2 classes:  Buildings class and Rooms class. The buildings class contain a one2many field called rooms (so that a building can contain multiple rooms)

I want to show in the view form of the building a list of rooms of the building and in each line a button to open the room form. I managed to show the button but how do I tell the button the right (the one that was clicked) room to open ?

I have an example that doesn't work for me


class Buildings(models.Model):
_name = 'my_module.buildings'
    name = fields.Char()
    rooms = fields.One2many('my_module.rooms', 'building_id')

class Rooms(models.Model):
_name = 'my_module.rooms'
name = fields.Char()
building_id = fields.Many2one('my_module.buildings', required=True)

# this method should open a form for the clicked room
def open_one2many_line(self, context=None):
        return {
            'type': 'ir.actions.act_window',
     'name': 'Open Line',
     'view_type': 'form',
 'view_mode': 'form',
 'res_model': self._name,
 'res_id': context('active_id'),
 'target': 'current',
 }

The view.xml file

<record id="building_view_form" model="ir.ui.view">
<field name="name">my_module.buildings.form</field>
<field name="model">my_module.buildings</field>
<field name="arch" type="xml">
<form string="Buildings Form">
<sheet>
<group>
<field name="name"/>
<field name="rooms">
<tree>
<field name="name"/>
<button
string="open"
type="object"
name="open_one2many_line"
context="{'default_active_id': active_id}"
>
</button>
</tree>
</field>
</group>
</sheet>
</form>
</field>
</record>


Avatar
Discard

You need to pass id of 'room' on relevant line .

Author

on the button tag in the view.xml file? How do I pass the id? Should I use the context?

Best Answer
def open_one2many_line(self):
context = self.env.context
return {
'type': 'ir.actions.act_window',
'name': 'Open Line',
'view_type': 'form',
'view_mode': 'form',
'res_model': self._name,
'res_id': context.get('default_active_id'),
'target': 'new',
}
Avatar
Discard