gpt4 book ai didi

python - 奥杜 10 : How to know if record is already in database or a new one?

转载 作者:行者123 更新时间:2023-12-01 00:05:16 25 4
gpt4 key购买 nike


我正在计算 x 记录的平均值,但我不想包括最后一个记录(触发操作的记录)。我可以在现有记录或新记录中触发该操作(尚未在数据库)。
这是我的代码:

@api.one
@api.depends('stc')
def _compute_average_gross(self):
if self.stc:
base_seniority = 12
match_seniority = self.seniority.split()
total_seniority = int(match_seniority[0]) + int(match_seniority[2]) * 12
if total_seniority < 12:
base_seniority = total_seniority if total_seniority else 1 # avoid dividing by 0
# if the hr.payslip is already in db
if self._origin.id:
limit = 13
# could be self.env.cr.execute()
sum_sbr = sum(self.search([('employee_id', '=', self.employee_id.id)], order='create_date desc', limit=limit)[1:].mapped('line_ids').filtered(lambda x: x.code == 'SBR').mapped('amount'))
sum_average_gross = sum(self.search([('employee_id', '=', self.employee_id.id)], order='create_date desc', limit=limit)[1:].mapped('average_gross'))
else:
limit = 12
# could be self.env.cr.execute()
sum_sbr = sum(self.search([('employee_id', '=', self.employee_id.id)], order='create_date desc', limit=limit).mapped('line_ids').filtered(lambda x: x.code == 'SBR').mapped('amount'))
sum_average_gross = sum(self.search([('employee_id', '=', self.employee_id.id)], order='create_date desc', limit=limit).mapped('average_gross'))
self.average_gross = round((sum_sbr + sum_average_gross) / base_seniority, 2)

这样我就得到了一个错误,表明 self 没有 _origin,我尝试使用 origin 但得到了相同的错误。我也尝试过 self.context['params'].get('id') 但它没有按预期工作。
你能帮我吗?

最佳答案

要检查记录是否未保存在数据库中,请执行以下操作:

if isinstance(self.id, models.NewId): 
# record is not saved in database.
# do your logic

# record is saved in databse
if not isinstance(self.id, models.NewId):
# ....

关于python - 奥杜 10 : How to know if record is already in database or a new one?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60024530/

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