feat: 获取子集的性能优化及删除提示

This commit is contained in:
caoqianming 2024-06-05 12:50:47 +08:00
parent 7b1013ea64
commit a54030e9c0
2 changed files with 8 additions and 0 deletions

View File

@ -171,6 +171,8 @@ class BulkDestroyModelMixin(DestroyModelMixin):
raise ValidationError(**PKS_ERROR)
else:
instance = self.get_object()
if hasattr(instance, "parent") and instance.__class__.objects.filter(parent=instance).exists():
raise ParseError('存在子级,无法删除')
self.perform_destroy(instance)
return Response(status=204)

View File

@ -52,9 +52,15 @@ def get_child_queryset2(obj, hasParent=True):
obj实例
数据表需包含parent字段
是否包含父默认True
若有parent_link字段则进行性能优化
'''
cls = type(obj)
queryset = cls.objects.none()
if hasattr(cls, 'parent_link'):
queryset = cls.objects.filter(parent_link__contains=obj.id)
if hasParent:
queryset = queryset | cls.objects.filter(pk=obj.id)
return queryset
if hasParent:
queryset = cls.objects.filter(pk=obj.id)
child_queryset = cls.objects.filter(parent=obj)