我目前正在使用hr_holidays
(又名Leave Management
)模块,我需要将假期分配添加为给定类型假期的唯一记录。
我在create()
函数中添加了一个额外条件,导致不允许任何请假请求。
以下是我的create()
功能:
def create(self, cr, uid, values, context=None):
#-------what I have added starting from here------
hol_stat=values['holiday_status_id']
emp_id=values['employee_id']
n_allo=values['test_allocation']
ids=self.pool.get('hr.holidays').search(cr, uid, [('holiday_status_id','=', hol_stat),('employee_id','=', emp_id),('type','=','add')], context=context)
if ids:
raise osv.except_osv(_('Warning!'),_('Already allocated leaves .'))
# ------end from here-----
""" Override to avoid automatic logging of creation """
if context is None:
context = {}
context = dict(context, mail_create_nolog=True)
if ('date_from' in values and 'date_to' in values):
define_holidays = self.pool.get('company.holidays').search(cr, uid, ['|',('date', '=', values['date_from']),('date', '=', values['date_to'])])
if define_holidays:
raise osv.except_osv(_('Warning!'),_('You need not to apply leaves one Company holidays.'))
else:
return super(hr_holidays, self).create(cr, uid, values, context=context)
else:#mani
return super(hr_holidays, self).create(cr, uid, values, context=context)
问题
如何更改create()
功能以达到唯一记录要求?
答案 0 :(得分:1)
不是在您想要创建新记录的任何时候进行搜索,而是在数据库上强制执行UniqueConstraint
,而不允许使用此
_sql_constraints = [
('unique_hol_empid_type', 'unique(holiday_status_id, employee_id, type)', 'Error: Already allocated leaves'),
]
唯一约束是您想要的三个列的组合,unique_hol_empid_type
是我们为约束(在数据库层)提供的名称,并且只要记录违反该约束,就会引发错误。< / p>