diff --git a/apps/wpm/models.py b/apps/wpm/models.py index bfd47df2..13c397fa 100644 --- a/apps/wpm/models.py +++ b/apps/wpm/models.py @@ -665,17 +665,15 @@ class BatchSt(BaseModel): """ if mioitem: mio = mioitem.mio - try: - node = cls.objects.get(batch=batch, version=1) - if check_mat_start: - if node.material_start is None: - node.material_start = material_start - node.save(update_fields = ["material_start"]) - if node.material_start != material_start: - raise ParseError(f"{batch}-该批次号因物料不同不可引用") - return node, False - except cls.DoesNotExist: - return cls.objects.create(batch=batch, mio=mio, mioitem=mioitem, handover=handover, mlog=mlog, material_start=material_start, version=1), True + node, created = cls.safe_get_or_create(batch=batch, version=1, defaults={ + "mio":mio, "mioitem":mioitem, "handover":handover, "mlog":mlog, "material_start":material_start}) + if not created and check_mat_start: + if node.material_start is None: + node.material_start = material_start + node.save(update_fields = ["material_start"]) + if node.material_start != material_start: + raise ParseError(f"{batch}-该批次号因物料不同不可引用") + return node, created # if mio is None and handover is None and mlog is None: # try: @@ -778,9 +776,15 @@ class BatchLog(BaseModel): if target.mio is None and target.mioitem is None: target.delete() if mio: - BatchSt.objects.filter(mio=mio).delete() + ts = BatchSt.objects.filter(mio=mio) + for t in ts: + if not BatchLog.objects.filter(Q(source=t) | Q(target=t)).exists(): + t.delete() if mioitem: - BatchSt.objects.filter(mioitem=mioitem).delete() + ts = BatchSt.objects.filter(mioitem=mioitem) + for t in ts: + if not BatchLog.objects.filter(Q(source=t) | Q(target=t)).exists(): + t.delete() @classmethod def batches_to(cls, batch:str):