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才能解决。

相关推荐
骇客野人34 分钟前
使用python写一套完整的智能体小程序
开发语言·python
山楂树の1 小时前
模型优化——在MacOS 上使用 Python 脚本批量大幅度精简 GLB 模型(通过 Blender 处理)
python·macos·3d·图形渲染·blender
云霄IT2 小时前
python之使用ffmpeg下载直播推流视频rtmp、m3u8协议实时获取时间进度
python·ffmpeg·音视频
沐风清扬2 小时前
Win10下python环境变量呼出微软应用商店
开发语言·python
java1234_小锋2 小时前
【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 微博评论数据可视化分析-点赞区间折线图实现
python·自然语言处理·flask
MobiCetus3 小时前
确保conda环境内的Py不会污染系统
chrome·python·conda
树獭叔叔4 小时前
Python 多进程与多线程:深入理解与实践指南
后端·python
CodeCraft Studio5 小时前
国产化PDF处理控件Spire.PDF教程:Java 提取 PDF 图片,高质量提取与图片过滤技巧
java·python·pdf·国产化·文档处理·spire·pdf图片提取
去伪存真5 小时前
前端get到的新技能--手把手教你使用Python实现查询基金年度排名功能
前端·python
PixelMind5 小时前
【IQA技术专题】DISTS代码讲解
图像处理·人工智能·python·算法·iqa