feat: 获取子集的性能优化及删除提示
This commit is contained in:
parent
7b1013ea64
commit
a54030e9c0
|
@ -171,6 +171,8 @@ class BulkDestroyModelMixin(DestroyModelMixin):
|
||||||
raise ValidationError(**PKS_ERROR)
|
raise ValidationError(**PKS_ERROR)
|
||||||
else:
|
else:
|
||||||
instance = self.get_object()
|
instance = self.get_object()
|
||||||
|
if hasattr(instance, "parent") and instance.__class__.objects.filter(parent=instance).exists():
|
||||||
|
raise ParseError('存在子级,无法删除')
|
||||||
self.perform_destroy(instance)
|
self.perform_destroy(instance)
|
||||||
return Response(status=204)
|
return Response(status=204)
|
||||||
|
|
||||||
|
|
|
@ -52,9 +52,15 @@ def get_child_queryset2(obj, hasParent=True):
|
||||||
obj实例
|
obj实例
|
||||||
数据表需包含parent字段
|
数据表需包含parent字段
|
||||||
是否包含父默认True
|
是否包含父默认True
|
||||||
|
若有parent_link字段则进行性能优化
|
||||||
'''
|
'''
|
||||||
cls = type(obj)
|
cls = type(obj)
|
||||||
queryset = cls.objects.none()
|
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:
|
if hasParent:
|
||||||
queryset = cls.objects.filter(pk=obj.id)
|
queryset = cls.objects.filter(pk=obj.id)
|
||||||
child_queryset = cls.objects.filter(parent=obj)
|
child_queryset = cls.objects.filter(parent=obj)
|
||||||
|
|
Loading…
Reference in New Issue