本文介绍了Django REST框架:在删除之前验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想在对象被删除之前运行一个验证,以防在某些情况下被删除并作为验证错误返回。我怎么做?我目前看起来不是正确的:

I want to run a validation before an object is deleted, to prevent deletion in certain cases and return as a validation error. How do I do that? What I have currently doesn't seem right:

class CallDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = XCall.objects.all()
    serializer_class = CallSerializer
    ...

    def pre_delete(self, obj):
        if obj.survey:
            raise serializers.ValidationError("Too late to delete")


推荐答案

提出一个 ValidationError ,我只会提出一个或适合错误描述的其他自定义错误:

Rather than raising a ValidationError, I will just raise a ParseError or another custom error that fits the error description:

from rest_framework import exceptions
def pre_delete(self, obj):
        if obj.survey:
            raise exceptions.ParseError("Too late to delete")

这篇关于Django REST框架:在删除之前验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:01