From c3bb68acae192aa5662fad438293264ae01b4af2 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 26 Aug 2024 15:28:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20base=20basemodel=20handle=5Fparent=20?= =?UTF-8?q?=E5=A4=84=E7=90=86bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/utils/models.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/apps/utils/models.py b/apps/utils/models.py index 3a18d461..a1afd64c 100755 --- a/apps/utils/models.py +++ b/apps/utils/models.py @@ -124,13 +124,16 @@ class BaseModel(models.Model): old_parent = None need_handle_parent = False if hasattr(self, "parent"): - try: - old_parent = self.__class__.objects.get(id=self.id).parent - except Exception: - self.parent = None - need_handle_parent = True - if self.parent != old_parent: + if is_create: need_handle_parent = True + else: + try: + old_parent = self.__class__.objects.get(id=self.id).parent + except Exception: + self.parent = None + need_handle_parent = True + if self.parent != old_parent: + need_handle_parent = True try: ins = super().save(*args, **kwargs) except IntegrityError as e: