perform_destroy() 上的个人回复消息

individual Response message on perform_destroy()

我写了一个自定义 perform_destroy() 方法来做一些额外的检查。

def perform_destroy(self,instance):
    force = self.request.data.get('force',None)
    resp = {}
    if Processingstepstatus.objects.filter(orderfile__order=instance).exists() and not force:
        raise serializers.ValidationError({"message": "Order already in production and can't be canceled cost-free. Perform Delete Request with parameter \"force:True\" to cancel and assume the costs of "+ str(instance.currentcosts)+"€" })
    else:
        if Processingstepstatus.objects.filter(orderfile__order=instance).exists() and force:
            resp['invoice'] = "Placeholdervalue"
        instance.canceled = True
        instance.save()
        resp['message'] = "Successfully canceled your order."
        return Response(resp,status=status.HTTP_200_OK)

这应该会返回一个 json 响应消息和发票信息,但除了 204 - 无内容外,它不会给出任何响应。

我觉得这里是被更高级的方法destroy()覆盖了吧? 如何处理?

此致

来自所有 perform_<method> 方法的 return 值将被忽略,如果您希望 return 自定义响应,您应该覆盖 destroy 方法

def destroy(self, request, *args, **kwargs):
    instance = self.get_object()
    force = request.data.get('force',None)
    resp = {}
    if Processingstepstatus.objects.filter(orderfile__order=instance).exists() and not force:
        raise serializers.ValidationError({"message": "Order already in production and can't be canceled cost-free. Perform Delete Request with parameter \"force:True\" to cancel and assume the costs of "+ str(instance.currentcosts)+"€" })
    else:
        if Processingstepstatus.objects.filter(orderfile__order=instance).exists() and force:
            resp['invoice'] = "Placeholdervalue"
        instance.canceled = True
        instance.save()
        resp['message'] = "Successfully canceled your order."
        return Response(resp,status=status.HTTP_200_OK)