fix: base basemodel handle_parent 处理bug

This commit is contained in:
caoqianming 2024-08-26 15:28:13 +08:00
parent ac6fd28aba
commit b57fd16d5a
1 changed files with 9 additions and 6 deletions

View File

@ -124,13 +124,16 @@ class BaseModel(models.Model):
old_parent = None old_parent = None
need_handle_parent = False need_handle_parent = False
if hasattr(self, "parent"): if hasattr(self, "parent"):
try: if is_create:
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 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: try:
ins = super().save(*args, **kwargs) ins = super().save(*args, **kwargs)
except IntegrityError as e: except IntegrityError as e: