Django过滤器返回无限循环

时间:2019-11-28 09:35:36

标签: django

当我过滤公司的仓库时,它返回错误: RecursionError: maximum recursion depth exceeded in comparison

def destroy(self, request, *args, **kwargs):
    company = Company.objects.get(owner=self.request.user)
    stores = Store.objects.filter(company=company.id)
    if len(stores) <= 1:
        return Response(data={'detail': 'At least one storehouse is required'}, status=status.HTTP_400_BAD_REQUEST)
    return self.delete(request, *args,**kwargs)

1 个答案:

答案 0 :(得分:0)

如果您使用的是DestroyAPIView,则调用self.delete()会将其返回到destroy method。而不是self.delete(),您应该调用super方法。

def destroy(self, request, *args, **kwargs):
    company = Company.objects.get(owner=self.request.user)
    stores = Store.objects.filter(company=company.id)
    if len(stores) <= 1:
        return Response(data={'detail': 'At least one storehouse is required'}, status=status.HTTP_400_BAD_REQUEST)
    return super().destroy(request, *args,**kwargs)