Elegant way to translate custom error message django rest framework

Viewed 336

I want to build 2 dual languages API. I use a custom exception handler and try catch errors on serializer like this (stripped a lot of boilerplate code). Client will receive the error code and lookup on it own dictionary.

I want to know are there better way to do it.

class OrgDuplicateName(APIException):
    status_code = 500 # not 201
    default_detail = ErrorCode.OrgDuplicateName.name
    default_code = ErrorCode.OrgDuplicateName


class OrganizationSerializer(serializers.ModelSerializer):


    class Meta:
        model = Organization
        fields = "__all__"

    def is_valid(self, raise_exception=False):
            if self._context["request"]._stream.method == "POST":
                if Organization.objects.filter(name=self.initial_data["name"]).exists():
                    raise OrgDuplicateName
                    break
                else:
                    return True
0 Answers
Related