odoo-054 one2many 字段新增时检查上一行某个字段是否填写

文章目录

需求描述

客户要求在 one2many 字段 job_lines 新增一行记录的时候要检查,上一行记录中的 datetime 字段 in_dt 是否填写,没有填写就警告不允许新增。

实现方法

一、在write()保存的时候检查

python 复制代码
def check_job_lines(self, job_lines):
    exist_in_dt = {}
    for i in self.job_lines:
        if str(i.id) not in exist_in_dt and i.in_dt:
            exist_in_dt[str(i.id)] = i.in_dt
    exist_create_date = False
    del_line = 0
    for w in job_lines:
        crud = w[0]
        if crud == 0:  # create
            exist_create_date = True
        elif crud == 1:  # update
            update_id = w[1]
            if 'in_dt' in w[2]:
                in_dt = w[2].get('in_dt', False)
                # 如果修改 in_dt 置空
                if in_dt:
                    exist_in_dt[str(update_id)] = in_dt
                else:
                    exist_in_dt.pop(str(update_id), None)
        elif crud == 2:  # delete
            update_id = w[1]
            # 如果删除 line 处理
            exist_in_dt.pop(str(update_id), None)
            del_line += 1
    if exist_create_date:
        if len(exist_in_dt.keys()) != (len(self.job_lines) - del_line):
            raise UserError(_('Please write  job order\'s In Datetime first!'))

然后在 write 方法中调用该方法。

二、onchange 在点击的时候直接提示

python 复制代码
@api.onchange('job_lines')
def onchange_job_lines(self):
    old_lines = []
    new_lines = []
    for i in self.job_lines:
        if not i._origin.id:
            new_lines.append(i)
        else:
            old_lines.append(i)
    if not old_lines:  # create lines first
        return True

    if len(new_lines) == 1:
        recent_lines = old_lines[-1]
        if not recent_lines.in_dt:
            return {
                'warning': {
                    'title': "Warning",
                    'message': "Please fill in the in_dt of the previous record first!",
                },
                'value': {'job_lines': [(6, 0, self.warranty_job_lines.ids)]}
            }
    else:
        for n in new_lines[1:]:
            if not n.in_dt:
                return {
                    'warning': {
                        'title': "Warning",
                        'message': "Please fill in the in_dt of the previous record first!",
                    },
                    'value': {'job_lines': [(6, 0, self.job_lines.ids)]}
                }

此方法有个bug

如果是同时添加两条记录的时候,同时第一条记录的 in_dt 没有填写,这时候应该是保留第一条记录,但是现在是会把新增的都给清空,这个是不是js才能解决。

相关推荐
万邦科技Lafite8 小时前
实战演练:利用京东API一键抓取商品详情
数据库·redis·python·缓存·开放api·淘宝开放平台
TheRouter8 小时前
OpenClaw 上下文瘦身:3 个实验
开发语言·python·ai
日光明媚9 小时前
TensorRT-LLM 中对 wan 加速流程与方法
人工智能·python·计算机视觉·stable diffusion·aigc
Promising_GEO9 小时前
全球综合评估模型-GCAM模型的安装与参数解读
开发语言·python·遥感·空间分析
TechWayfarer10 小时前
IP归属地API实战指南:用IP数据云解析日志挖掘用户地域分布
大数据·开发语言·网络·python·tcp/ip
Cloud_Shy61810 小时前
Python 数据分析基础入门:《Excel Python:飞速搞定数据分析与处理》学习笔记系列(第十一章 Python 包跟踪器 中篇)
数据库·python·sql·数据分析·excel·web
端平入洛11 小时前
Python 可变对象与引用穿透:为什么改了"里面的东西"外面也变了?
python
woon11 小时前
从“涂掉红色”到“删除 PDF 对象”:一次 PDF 去印章脚本改造实践
python
老纪11 小时前
c++怎么利用std--variant处理多种二进制子协议包的自动分支解析【进阶】
jvm·数据库·python
茗创科技11 小时前
Nat Hum Behav | 特征选择会导致基于脑影像的机器学习生物标志物产生迥异的神经生物学解释
python·深度学习·机器学习·matlab·脑网络