Search code examples
odoo

How to add a button or a link to re-direct to another model inside a tree view odoo 12?


I've tried to do giving widget ='url' in XML, Actually what I'm trying to do is, I have a model named A and B, A has the relation to B as One2many. So I wanna load to the Model B from the tree view in the Model A.(like from a form view when I click a button it loads the model B, like that from the tree view can I achieve?

class test(models.Model):
    _name = 'consultation'

    test_id = fields.One2many('case.sheet','consultation_id',string='Case Sheet Id')

Solution

  • When you click on a line in the tree view, Odoo will load the default form view to show the corresponding line record.

    The url widget uses the field value as the value of href attribute ( if the we do not specify the text attribute it will use the field value).

    button element can be a children element of the list view and to show a custom form view different from the one used in the tree view, you can add a button of type object in the tree view of the test_id field and declare a method with the same name in case.sheet.

    Example:

    Declare the method in case.sheet model:

    class CaseSheet(models.Model):
        _name = 'case.sheet'
    
        @api.multi
        def open_form_view(self):
            self.ensure_one()
            form_view = self.env.ref('MODULE.XML_VIEW_ID')
            return {
                'name': _('Case sheet'),
                'res_model': 'case.sheet',
                'res_id': self.id,
                'views': [(form_view.id, 'form'), ],
                'type': 'ir.actions.act_window',
                'target': 'new',
            }
    

    Add the button to the tree view:

    <field name="test_id">
        <tree editable="bottom">
            <field name="name"/>
            <button name="open_form_view" type="object" string="View" class="oe_highlight"/>
        </tree>
    </field>