Use model_dump(exclude_unset=True, by_alias=True) to get camelCase keys when constructing EditWarehouseSampleModel

Check for 'sampleId' (camelCase) instead of 'sample_id'
For new samples, use a separate model_dump() without by_alias=True to get snake_case keys for the DO object
main
risingLee 2026-01-05 15:22:58 +08:00
parent 4d4908bb2f
commit c72c71ff13
1 changed files with 6 additions and 5 deletions

View File

@ -170,11 +170,11 @@ class WarehouseReceiptService:
# 处理前端传来的样品 # 处理前端传来的样品
submitted_sample_ids = set() submitted_sample_ids = set()
for sample_data in receipt_model.samples: for sample_data in receipt_model.samples:
sample_dict = sample_data.model_dump(exclude_unset=True) sample_dict = sample_data.model_dump(exclude_unset=True, by_alias=True)
# 如果有sample_id说明是更新现有样品 # 如果有sample_id说明是更新现有样品
if 'sample_id' in sample_dict and sample_dict['sample_id']: if 'sampleId' in sample_dict and sample_dict['sampleId']:
sample_id = sample_dict['sample_id'] sample_id = sample_dict['sampleId']
submitted_sample_ids.add(sample_id) submitted_sample_ids.add(sample_id)
# 更新样品 # 更新样品
from module_admin.entity.vo.warehouse_sample_vo import EditWarehouseSampleModel from module_admin.entity.vo.warehouse_sample_vo import EditWarehouseSampleModel
@ -183,8 +183,9 @@ class WarehouseReceiptService:
edit_sample.update_by = receipt_model.update_by edit_sample.update_by = receipt_model.update_by
await WarehouseSampleDao.edit_sample(db, edit_sample) await WarehouseSampleDao.edit_sample(db, edit_sample)
else: else:
# 新增样品 # 新增样品 - 转换为snake_case用于创建DO对象
sample = WarehouseSample(**sample_dict) sample_dict_snake = sample_data.model_dump(exclude_unset=True)
sample = WarehouseSample(**sample_dict_snake)
sample.receipt_id = receipt_model.receipt_id sample.receipt_id = receipt_model.receipt_id
sample.receipt_no = receipt_model.receipt_no or receipt.receipt_no sample.receipt_no = receipt_model.receipt_no or receipt.receipt_no
sample.create_by = receipt_model.update_by sample.create_by = receipt_model.update_by