且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django REST框架:在删除之前验证

更新时间:2023-12-04 09:34:58

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

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")