当我使用Odoo v11单击res.partner中的按钮时,如何显示另一个模型的树视图?

时间:2018-03-31 13:25:29

标签: python python-3.x odoo odoo-view odoo-11

我在表单视图中有一个继承base.view_partner_form的按钮。当我单击按钮时,我想显示另一个模型的树视图。我已经尝试了但是我收到错误External ID not found错误。我该如何解决?这是我的代码:

表单视图:

<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>

树状视图:

<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文件,按钮代码:

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文件,新模型:

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

    similar_name = fields.Char("Name")

    similar_email = fields.Char("Email")

    similar_phone = fields.Char("Phone")

1 个答案:

答案 0 :(得分:1)

试试这个:

@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'
    }

注意:请考虑应调用自定义模块similar_task