如何在 odoo 中显示自定义搜索结果?
How do I show custom search results in odoo?
我已经在我的程序模型中创建了这个方法:
@api.multi
def get_results(self):
q = "select * from program_program where id_year = 1"
self.env.cr.execute(q)
res = self.env.cr.dictfetchall()
result = self.env['program.program'].browse([row['id_viti'] for row in res])
print(result)
return result
我已经创建了一个动作服务器:
<record id="program" model="ir.actions.server">
<field name="name">First year program</field>
<field name="condition">True</field>
<field name="type">ir.actions.server</field>
<field name="model_id" ref="model_program_program" />
<field name="state">code</field>
<field name="code">action = model.get_results()</field>
</record>
还有一个菜单项:
<menuitem id="year_1"
name="First year"
action="program"
/>
因此 get_results
方法在菜单项单击时被调用。我希望能够在我的页面上显示该方法的结果。我收到此错误:
AttributeError: 'program.program' object has no attribute 'setdefault'
您的方法应该return一个window动作而不是记录集(搜索结果),并使用域来过滤记录:
@api.multi
def get_results(self):
# just to simplify the code using Odoo APIs
ids = self.env['program.program'].search([('id_year','=', 1)]).mapped('id_viti.id')
return {
'name':_("Programs of year 1"),
'view_mode': 'tree,form',
'view_type': 'form',
'res_model': 'program.program',
'type': 'ir.actions.act_window',
'domain': [('id', 'in', ids)],
'context': self._context,
}
对于任何语法错误,我们深表歉意。
我已经在我的程序模型中创建了这个方法:
@api.multi
def get_results(self):
q = "select * from program_program where id_year = 1"
self.env.cr.execute(q)
res = self.env.cr.dictfetchall()
result = self.env['program.program'].browse([row['id_viti'] for row in res])
print(result)
return result
我已经创建了一个动作服务器:
<record id="program" model="ir.actions.server">
<field name="name">First year program</field>
<field name="condition">True</field>
<field name="type">ir.actions.server</field>
<field name="model_id" ref="model_program_program" />
<field name="state">code</field>
<field name="code">action = model.get_results()</field>
</record>
还有一个菜单项:
<menuitem id="year_1"
name="First year"
action="program"
/>
因此 get_results
方法在菜单项单击时被调用。我希望能够在我的页面上显示该方法的结果。我收到此错误:
AttributeError: 'program.program' object has no attribute 'setdefault'
您的方法应该return一个window动作而不是记录集(搜索结果),并使用域来过滤记录:
@api.multi
def get_results(self):
# just to simplify the code using Odoo APIs
ids = self.env['program.program'].search([('id_year','=', 1)]).mapped('id_viti.id')
return {
'name':_("Programs of year 1"),
'view_mode': 'tree,form',
'view_type': 'form',
'res_model': 'program.program',
'type': 'ir.actions.act_window',
'domain': [('id', 'in', ids)],
'context': self._context,
}
对于任何语法错误,我们深表歉意。