简体   繁体   中英

How to show a tree view of another model when I click a button in res.partner with Odoo v11?

I have a button in form view that is inheriting base.view_partner_form . When I click the button, I want to show the tree view of another model. I have tried it but I get the error External ID not found error is appeared. How can I fix it? Here is my code:

Form View:

<record id="similar_list_view_partner_form" model="ir.ui.view">
    <field name="name">similar_list_view_partner_form</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form"></field>
    <field name="arch" type="xml">
        <xpath expr="//button[@name='toggle_active'][not(ancestor::field)]" position="after">
            <button class="oe_stat_button" name="similar_list_button_action" type="object" icon="fa-align-justify"> 
                <field name="show" readonly="1"/>
            </button>
        </xpath>
    </field>
</record>

Tree View:

<record id="similar_detail_tree_view" model="ir.ui.view">
    <field name="name">Similar Detail Tree View</field>
    <field name="model">similar.task</field>
    <field name="arch" type="xml">
        <tree>
            <field name="similar_name"/>
            <field name="similar_email"/>
            <field name="similar_phone"/>
        </tree>
    </field>
</record>

Python file, button code:

class similar_contact(models.Model):
    _inherit = 'res.partner'

    show = fields.Char(
        string='Similar'
    )

    @api.multi
    def similar_list_button_action(self):
        tree_view_id = self.env.ref('similar_task.similar_detail_tree_view').id
        return {
            'name':'Similar Detail Tree View',
            'type':'ir.actions.act_window',
            'res_model':'similar.task',
            'view_mode':'tree',
            'views':[(view_tree_id,'tree')],
            'res_id':False,
            'target':'new'
        }

Python file, new model:

class similar_detail(models.Model):
    _name="similar.task"

    similar_name = fields.Char("Name")

    similar_email = fields.Char("Email")

    similar_phone = fields.Char("Phone")

Try this:

@api.multi
def similar_list_button_action(self):
    tree_view_id = self.env.ref('similar_task.similar_detail_tree_view').id       
    return {
        'name': _('Similar Detail Tree View'),
        'type': 'ir.actions.act_window',
        'res_model': 'similar.task',
        'view_type':'form',
        'view_mode': 'tree',
        'view_id': tree_view_id,
        'res_id': False,
        'context': False,
        'target':'new'
    }

Note: Take into account that your custom module should be called similar_task

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM