如何在 python 中初始化 ir.actions.act_window 的参数 search_view_id

How to initialize parameter search_view_id of ir.actions.act_window in python

我通过 xml

中的按钮调用此 python 方法
def plots(self):
    self.ensure_one()
    product = self.env['product.product'].search([('product_tmpl_id','=',self.id)])
    domain = [('type', '=', 'is_plot')]
    return {
        'name': _('Plots'),
        'res_model': 'product.template',
        'type': 'ir.actions.act_window',
        'domain': domain,
        'view_mode': 'kanban,tree,form',
        'view_type': 'form',
        'search_view_id': ,
        'help': _('''<p class="oe_view_nocontent_create">
                    Plots of Land....</p>'''),
        'context': "{'default_res_model': '%s','default_res_id': %d}" % (self._name, self.id)
    }

现在我想要的是像这样引用 xml 文件中定义的搜索视图

<record id="view_land_plots_search" model="ir.ui.view">
    <field name="name">land.plot.search</field>
    <field name="model">product.template</field>
    <field name="arch" type="xml">
        <search string="Land Plots">
            <group expand="1" string="Group By">
                <filter string="Stage" name='stage_id' context="{'group_by': 'stage_id'}"/>
            </group>
        </search>
    </field>
</record>

我不知道如何初始化search_view_id。

在 Odoo python 代码中引用使用 xml 创建的任何记录可以使用记录 xml_idself.env.ref() 方法完成。

在这种情况下,您的搜索查看记录的 xml_idyour_module_name.view_land_plots_search。所以 python 代码将是:

'search_view_id': self.env.ref('your_module_name.view_land_plots_search').id,
'context': "{'search_default_filter_stage_id': 1}"

不要忘记将 your_module_name 替换为您的自定义模块目录名称。

<record id="view_land_plots_search" model="ir.ui.view">
    <field name="name">land.plot.search</field>
    <field name="model">product.template</field>
    <field name="arch" type="xml">
        <search string="Land Plots">
            <group expand="1" string="Group By">
                <filter string="Stage" name='filter_stage_id' context="{'group_by': 'stage_id'}"/>
            </group>
        </search>
    </field>
</record>