gpt4 book ai didi

python - 覆盖 OpenERP 约束

转载 作者:行者123 更新时间:2023-11-30 23:36:52 25 4
gpt4 key购买 nike

我想编写一个模块来重写模型对象上的约束之一,但仅重写约束方法是行不通的。我对该方法的重写永远不会被调用,因为 OpenERP 使用它自己的继承机制。

我正在尝试在 hr_timesheet_sheet 模块中使有关登录/注销操作的规则更加灵活,以便员工可以在今天登录后记录前一天的工作时间。为此,我想覆盖 action 字段上的 hr.attendance 约束并允许任何更改,然后在时间表级别执行检查以确保所有出勤操作均已执行按逻辑顺序。

我找到了hr.attendance constraint .

def _altern_si_so(self, cr, uid, ids, context=None):
""" Alternance sign_in/sign_out check.
Previous (if exists) must be of opposite action.
Next (if exists) must be of opposite action.
"""
for att in self.browse(cr, uid, ids, context=context):
# search and browse for first previous and first next records
prev_att_ids = self.search(cr, uid, [('employee_id', '=', att.employee_id.id), ('name', '<', att.name), ('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='name DESC')
next_add_ids = self.search(cr, uid, [('employee_id', '=', att.employee_id.id), ('name', '>', att.name), ('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='name ASC')
prev_atts = self.browse(cr, uid, prev_att_ids, context=context)
next_atts = self.browse(cr, uid, next_add_ids, context=context)
# check for alternance, return False if at least one condition is not satisfied
if prev_atts and prev_atts[0].action == att.action: # previous exists and is same action
return False
if next_atts and next_atts[0].action == att.action: # next exists and is same action
return False
if (not prev_atts) and (not next_atts) and att.action != 'sign_in': # first attendance must be sign_in
return False
return True

_constraints = [(_altern_si_so, 'Error: Sign in (resp. Sign out) must follow Sign out (resp. Sign in)', ['action'])]

我尝试重写模块中的约束方法,但我的版本从未被调用。

def _altern_si_so(self, cr, uid, ids):
""" Implementing this logic at the attendance level doesn't work, so
we skip it, and check at the whole time sheet level. 's all good!"""
return True

我还尝试在同一字段上添加我自己的约束,但随后它调用了两个版本,并且基本模块的约束可能会拒绝保存。

最佳答案

我找到了bug on launchpad这描述了我遇到的问题和解决方法。事实证明,您现在可以覆盖约束,但这非常繁琐。您必须在与基本模块具有相同函数名称相同字段上声明自己的约束。然后,也只有到那时,它才会调用您的约束,而不是基本版本。

这是my module它会覆盖操作的 hr_attendance 约束并允许任意组合。

class hr_attendance(osv.osv):
_inherit = 'hr.attendance'

def _altern_si_so(self, cr, uid, ids):
""" Implementing this logic at the attendance level doesn't work, so
we skip it, and check at the whole time sheet level. 's all good!"""
return True

_constraints = [(_altern_si_so,
'Error: You should never see this message.',
['action'])]

关于python - 覆盖 OpenERP 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16088505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com