在窗口操作 odoo 中调用 Python 函数



我将这个python函数添加到"hr.holidays"模型中

class InheritHrHolidays(models.Model):
_inherit = 'hr.holidays'
def _get_holiday_status_id_domain(self):
if not self.env.user.has_group('hr_holidays.group_hr_holidays_user'):
allocate_type = self.env['hr.holidays.status'].search([('name', '=', 'Compensatory Days')], limit=1)
return [('id', '=', allocate_type.id)]
elif self.env.user.has_group('hr_holidays.group_hr_holidays_user') and not self.env.user.has_group(
'hr_holidays.group_hr_holidays_manager'):
allocation_types = self.env['hr.holidays.status'].search([('name', '!=', 'Unpaid')])
return [('id', 'in', allocation_types.mapped('id'))]

我想在操作 id="hr_holidays.open_allocation_holidays' 中调用此函数,以便在操作运行时仍执行。

<record id="open_allocation_holidays" model="ir.actions.act_window">
<field name="name">Allocation Request</field>
<field name="res_model">hr.holidays</field>
<field name="view_type">form</field>
<field name="view_mode">tree,kanban,form</field>
<field name="context">{
'default_type':'add',
'search_default_my_leaves': 1,
'needaction_menu_ref':
[
'hr_holidays.menu_open_company_allocation',
]
}</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click here to create a new leave allocation request.
</p>
</field>
<field name="domain">[('type','=','add')]</field>
<field name="view_id" ref="edit_holiday_new"/>
<field name="search_view_id" ref="view_hr_holidays_filter"/>
</record>

您不能在窗口操作中调用函数,而是必须创建例如服务器操作(使用代码(,然后应由菜单项调用或使用。在该服务器操作中,您可以使用原始窗口操作,但使用函数中的域进行操作。

服务器操作的代码应如下所示:

action = env.ref('hr_holidays.open_allocation_holidays').read()[0]
domain = model._get_holiday_status_id_domain()
action['domain'] = domain

最新更新