python - How to propagate error on create method in DRF serializer? - Stack Overflow

admin2025-04-08  1

I'm trying to raise some error based on some business logic as in below

class ConsultationViewset(BaseModelViewset, ListModelViewsetMixin, RetrieveModelViewsetMixin, CreateModelViewsetMixin, UpdateModelViewsetMixin):
    serializer_class = ConsultationSerializer
    ....
class ConsultationSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        consultation = ConsultationService.create_consultation(validated_data.pop('customer', None), validated_data)
        return consultation
def create_consultation(customer: CustomerFactory.Models.CUSTOMER, validated_data):
    if ticket is None:
            raise exceptions.PermissionDenied("No active ticket found for this customer, request is forbidden")

My aim is to send the raised message in create_consultation in the response. Yet I keep getting AssertionError: create() did not return an object instance.instead. I could send a custom message if I re-raise the error in the viewset like below, but it felt wrong as the error is AssertionError.

class ConsultationViewset(...):
    def perform_create(self, serializer):
        try:
            serializer.save()
        except AssertionError as e:
            raise exceptions.PermissionDenied('custom message')

How to properly raise a PermissionDenied error?

I'm trying to raise some error based on some business logic as in below

class ConsultationViewset(BaseModelViewset, ListModelViewsetMixin, RetrieveModelViewsetMixin, CreateModelViewsetMixin, UpdateModelViewsetMixin):
    serializer_class = ConsultationSerializer
    ....
class ConsultationSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        consultation = ConsultationService.create_consultation(validated_data.pop('customer', None), validated_data)
        return consultation
def create_consultation(customer: CustomerFactory.Models.CUSTOMER, validated_data):
    if ticket is None:
            raise exceptions.PermissionDenied("No active ticket found for this customer, request is forbidden")

My aim is to send the raised message in create_consultation in the response. Yet I keep getting AssertionError: create() did not return an object instance.instead. I could send a custom message if I re-raise the error in the viewset like below, but it felt wrong as the error is AssertionError.

class ConsultationViewset(...):
    def perform_create(self, serializer):
        try:
            serializer.save()
        except AssertionError as e:
            raise exceptions.PermissionDenied('custom message')

How to properly raise a PermissionDenied error?

Share Improve this question asked Mar 26 at 20:03 nama aslikunama asliku 1072 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

When you use Django REST Framework (DRF), the serializer's create() method will create and return a new object. The error happens as when the serializer calls create_consultation function but there's no ticket and raises an error. Because of this error, nothing gets returned from the create() method. That's why you get AssertionError: create() did not return an object instance

You can try to catch the error in the serializer

class ConsultationSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        try:
            consultation = ConsultationService.create_consultation(validated_data.pop('customer', None), validated_data)
            return consultation
        except exceptions.PermissionDenied as e:
            raise e
        except Exception as e:
            # Convert other exceptions to DRF exceptions if needed
            raise exceptions.ValidationError(str(e))
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744127834a232547.html

最新回复(0)