Home > database >  individual Response message on perform_destroy()
individual Response message on perform_destroy()

Time:10-14

I've written a custom perform_destroy()-method to do some additional checks.

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)

This should give back a json-response with the message and the invoice information but it doesn't give any response except 204 - no content.

I think it is being overwritten by the higher-level method destroy() here, right? How to handle that?

best regards

CodePudding user response:

The return values from all the perform_<method> methods are ignored, if you wish to return a custom response you should override the destroy method

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)
  • Related