From 301e886a9e5a07018c5e512ff9e3050c58389570 Mon Sep 17 00:00:00 2001 From: zty Date: Wed, 3 Jun 2026 14:45:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(hrm):=20=E5=AF=BC=E5=85=A5=E9=83=A8?= =?UTF-8?q?=E9=97=A8=E6=94=B9=E4=B8=BA=E4=BB=85=E6=96=B0=E5=A2=9E=E5=BF=85?= =?UTF-8?q?=E5=A1=AB=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=9B=B4=E6=96=B0=E8=A2=AB?= =?UTF-8?q?=E6=8B=A6=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前“所属部门必填”会拦掉部门列为空的更新行,导致已有员工无法被覆盖。 改为:填了才校验是否存在;为空时——更新保留原部门、新增才报错。 Co-Authored-By: Claude Opus 4.8 --- apps/hrm/views.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/hrm/views.py b/apps/hrm/views.py index 8c9dc02e..9b9cc201 100755 --- a/apps/hrm/views.py +++ b/apps/hrm/views.py @@ -419,13 +419,12 @@ class EmployeeViewSet(CustomModelViewSet): data['type'] = TYPE_MAPPING[excel_type] else: raise ParseError(f'第{row_num}行,人员类型"{excel_type}"无效,有效类型:{", ".join(TYPE_MAPPING.keys())}') - # 处理部门外键(所属部门必填) + # 处理部门外键:填了就校验是否存在并赋值;为空时不动(新增场景的必填在下方创建处校验) dept_name = data.pop('belong_dept', None) - if not dept_name: - raise ParseError(f'第{row_num}行,所属部门为空') - if dept_name not in dept_map: - raise ParseError(f'第{row_num}行,部门"{dept_name}"不存在') - data['belong_dept_id'] = dept_map[dept_name] + if dept_name: + if dept_name not in dept_map: + raise ParseError(f'第{row_num}行,部门"{dept_name}"不存在') + data['belong_dept_id'] = dept_map[dept_name] # 数据验证 if data.get('phone'): @@ -470,6 +469,9 @@ class EmployeeViewSet(CustomModelViewSet): myLogger.info(f"⏭️ 第{row_num}行无变化:{name}") created = False else: + # 新增人员时所属部门必填 + if 'belong_dept_id' not in data: + raise ParseError(f'第{row_num}行,新增人员时所属部门不能为空') Employee.objects.create(**data) created = True except Exception as e: