Custom error messages in Django Rest Framework

I have a serializer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class CompanyProfileCreateSerializer(serializers.ModelSerializer):
class Meta:
model = CompanyProfile
exclude = ["id", "company"]
class CompanyCreateSerializer(serializers.ModelSerializer):
company_profile = CompanyProfileCreateSerializer(required=True)
password = serializers.CharField(write_only=True)
class Meta:
model = Company
fields = ["id", "email", "password", "company_profile"]
extra_kwargs = {
"password": {"write_only": True, "style": {"input_type": "password"}}
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Define custom error messages for all fields dynamically
for field_name, field in self.fields.items():
field.error_messages.update({
"required": f"{field_name.replace('_', ' ').capitalize()} is required.",
"null": f"{field_name.replace('_', ' ').capitalize()} cannot be null.",
"invalid": f"Invalid value for {field_name.replace('_', ' ').capitalize()}."
})
def create(self, validated_data):
company_profile_data = validated_data.pop("company_profile")
company = Company.objects.create(**validated_data, **company_profile_data)
return company
</code>
<code>class CompanyProfileCreateSerializer(serializers.ModelSerializer): class Meta: model = CompanyProfile exclude = ["id", "company"] class CompanyCreateSerializer(serializers.ModelSerializer): company_profile = CompanyProfileCreateSerializer(required=True) password = serializers.CharField(write_only=True) class Meta: model = Company fields = ["id", "email", "password", "company_profile"] extra_kwargs = { "password": {"write_only": True, "style": {"input_type": "password"}} } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Define custom error messages for all fields dynamically for field_name, field in self.fields.items(): field.error_messages.update({ "required": f"{field_name.replace('_', ' ').capitalize()} is required.", "null": f"{field_name.replace('_', ' ').capitalize()} cannot be null.", "invalid": f"Invalid value for {field_name.replace('_', ' ').capitalize()}." }) def create(self, validated_data): company_profile_data = validated_data.pop("company_profile") company = Company.objects.create(**validated_data, **company_profile_data) return company </code>
class CompanyProfileCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = CompanyProfile
        exclude = ["id", "company"]


class CompanyCreateSerializer(serializers.ModelSerializer):
    company_profile = CompanyProfileCreateSerializer(required=True)
    password = serializers.CharField(write_only=True)

    class Meta:
        model = Company
        fields = ["id", "email", "password", "company_profile"]
        extra_kwargs = {
            "password": {"write_only": True, "style": {"input_type": "password"}}
        }
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Define custom error messages for all fields dynamically
        for field_name, field in self.fields.items():
            field.error_messages.update({
                "required": f"{field_name.replace('_', ' ').capitalize()} is required.",
                "null": f"{field_name.replace('_', ' ').capitalize()} cannot be null.",
                "invalid": f"Invalid value for {field_name.replace('_', ' ').capitalize()}."
            })

    def create(self, validated_data):
        company_profile_data = validated_data.pop("company_profile")
        company = Company.objects.create(**validated_data, **company_profile_data)
        return company

I added the __init__() method based on this answer to another question. But I am facing a problem.

If I send the following request:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"email": "[email protected]",
"password": "password123",
"company_profile": {
"name": "Company Test Register1"
}
}
</code>
<code>{ "email": "[email protected]", "password": "password123", "company_profile": { "name": "Company Test Register1" } } </code>
{
    "email": "[email protected]",
    "password": "password123",
    "company_profile": {
        "name": "Company Test Register1"
    }
}

the response I get is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"field": "email",
"detail": "user with this email already exists."
}
</code>
<code>{ "field": "email", "detail": "user with this email already exists." } </code>
{
    "field": "email",
    "detail": "user with this email already exists."
}

This is correct. The response is different from DRF’s default error response because I am using custom exception handler.

But the problem is that when I change the request to:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"password": "password123",
"company_profile": {
"name": "Company Test Register1"
}
}
</code>
<code>{ "password": "password123", "company_profile": { "name": "Company Test Register1" } } </code>
{
    "password": "password123",
    "company_profile": {
        "name": "Company Test Register1"
    }
}

I still get the same error response. The error response is the same until the server reloads. When in fact the error response should be:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"field": "email",
"detail": "Email is required"
}
</code>
<code>{ "field": "email", "detail": "Email is required" } </code>
{
    "field": "email",
    "detail": "Email is required"
}

But if I send the correct info in the request without the server reloading, it creates the company as expected.

Here are some more pieces of codes for more context:

view:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@extend_schema(tags=["company"])
class CompanyView(
GenericViewSet,
CreateModelMixin,
RetrieveModelMixin,
UpdateModelMixin,
):
"""View to create/retrieve/update a company."""
queryset = Company.objects.all()
lookup_field = "id"
parser_classes = [JSONParser, MultiPartParser, FormParser]
def get_serializer_class(self):
serializer_action_classes = {
"create": CompanyCreateSerializer,
"retrieve": CompanyRetrieveSerializer,
"update": CompanyUpdateSerializer,
"partial_update": CompanyUpdateSerializer,
}
if self.action in serializer_action_classes:
return serializer_action_classes[self.action]
else:
raise ValidationError({"detail": "Method Not Allowed."})
def get_permissions(self):
permission_action_classes = {
"create": [AllowAny()],
"retrieve": [IsAuthenticated(), IsOwner()],
"update": [IsAuthenticated(), IsOwner()],
"partial_update": [IsAuthenticated(), IsOwner()],
}
if self.action in permission_action_classes:
return permission_action_classes[self.action]
else:
return [NotAllowed()]
def get_serializer_context(self):
context = super().get_serializer_context()
if self.action in ["update", "partial_update"]:
context["instance"] = self.get_object()
return context
@extend_schema(description="Create a new company")
def create(self, request, *args, **kwargs):
return super().create(request, *args, **kwargs)
@extend_schema(description="Retrieve a single company by User ID")
def retrieve(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs)
@extend_schema(description="Update a single company by User ID")
def update(self, request, *args, **kwargs):
return super().update(request, *args, **kwargs)
@extend_schema(description="Partially Update a single company by User ID")
def partial_update(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
instance = serializer.update(
instance=Company.objects.get(pk=kwargs["id"]),
validated_data=serializer.validated_data,
)
output_serializer = CompanyRetrieveSerializer(
instance, context={"request": request}
)
return Response(output_serializer.data)
</code>
<code>@extend_schema(tags=["company"]) class CompanyView( GenericViewSet, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, ): """View to create/retrieve/update a company.""" queryset = Company.objects.all() lookup_field = "id" parser_classes = [JSONParser, MultiPartParser, FormParser] def get_serializer_class(self): serializer_action_classes = { "create": CompanyCreateSerializer, "retrieve": CompanyRetrieveSerializer, "update": CompanyUpdateSerializer, "partial_update": CompanyUpdateSerializer, } if self.action in serializer_action_classes: return serializer_action_classes[self.action] else: raise ValidationError({"detail": "Method Not Allowed."}) def get_permissions(self): permission_action_classes = { "create": [AllowAny()], "retrieve": [IsAuthenticated(), IsOwner()], "update": [IsAuthenticated(), IsOwner()], "partial_update": [IsAuthenticated(), IsOwner()], } if self.action in permission_action_classes: return permission_action_classes[self.action] else: return [NotAllowed()] def get_serializer_context(self): context = super().get_serializer_context() if self.action in ["update", "partial_update"]: context["instance"] = self.get_object() return context @extend_schema(description="Create a new company") def create(self, request, *args, **kwargs): return super().create(request, *args, **kwargs) @extend_schema(description="Retrieve a single company by User ID") def retrieve(self, request, *args, **kwargs): return super().retrieve(request, *args, **kwargs) @extend_schema(description="Update a single company by User ID") def update(self, request, *args, **kwargs): return super().update(request, *args, **kwargs) @extend_schema(description="Partially Update a single company by User ID") def partial_update(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=True) serializer.is_valid(raise_exception=True) instance = serializer.update( instance=Company.objects.get(pk=kwargs["id"]), validated_data=serializer.validated_data, ) output_serializer = CompanyRetrieveSerializer( instance, context={"request": request} ) return Response(output_serializer.data) </code>
@extend_schema(tags=["company"])
class CompanyView(
    GenericViewSet,
    CreateModelMixin,
    RetrieveModelMixin,
    UpdateModelMixin,
):
    """View to create/retrieve/update a company."""

    queryset = Company.objects.all()
    lookup_field = "id"
    parser_classes = [JSONParser, MultiPartParser, FormParser]

    def get_serializer_class(self):
        serializer_action_classes = {
            "create": CompanyCreateSerializer,
            "retrieve": CompanyRetrieveSerializer,
            "update": CompanyUpdateSerializer,
            "partial_update": CompanyUpdateSerializer,
        }
        if self.action in serializer_action_classes:
            return serializer_action_classes[self.action]
        else:
            raise ValidationError({"detail": "Method Not Allowed."})

    def get_permissions(self):
        permission_action_classes = {
            "create": [AllowAny()],
            "retrieve": [IsAuthenticated(), IsOwner()],
            "update": [IsAuthenticated(), IsOwner()],
            "partial_update": [IsAuthenticated(), IsOwner()],
        }
        if self.action in permission_action_classes:
            return permission_action_classes[self.action]
        else:
            return [NotAllowed()]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        if self.action in ["update", "partial_update"]:
            context["instance"] = self.get_object()
        return context

    @extend_schema(description="Create a new company")
    def create(self, request, *args, **kwargs):
        return super().create(request, *args, **kwargs)

    @extend_schema(description="Retrieve a single company by User ID")
    def retrieve(self, request, *args, **kwargs):
        return super().retrieve(request, *args, **kwargs)

    @extend_schema(description="Update a single company by User ID")
    def update(self, request, *args, **kwargs):
        return super().update(request, *args, **kwargs)

    @extend_schema(description="Partially Update a single company by User ID")
    def partial_update(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=True)
        serializer.is_valid(raise_exception=True)
        instance = serializer.update(
            instance=Company.objects.get(pk=kwargs["id"]),
            validated_data=serializer.validated_data,
        )
        output_serializer = CompanyRetrieveSerializer(
            instance, context={"request": request}
        )
        return Response(output_serializer.data)

custom exception handler function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from rest_framework.views import exception_handler
from rest_framework.response import Response
def get_last_value(dictionary: dict, values = []) -> list[list[str]]:
"""
Get the last values from a nested dictionary.
"""
for key, item in dictionary.items():
if isinstance(item, dict):
get_last_value(item, values)
else:
values.append(item)
return values
def custom_exception_handler(exc, context):
"""
Custom exception handler.
Format of exception:
{
"field": "Field name",
"detail": "Error message"
}
"""
response = exception_handler(exc, context)
if response is not None:
if isinstance(response.data, dict):
field = next(iter(response.data))
error_message = get_last_value(response.data)[0][0]
error_response = Response({
"field": field,
"detail": error_message
}, status=response.status_code)
return error_response
else:
error_message = str(response.data)
error_response = Response({
"field": "non_field_errors",
"detail": error_message
}, status=response.status_code)
return error_response
return response
</code>
<code>from rest_framework.views import exception_handler from rest_framework.response import Response def get_last_value(dictionary: dict, values = []) -> list[list[str]]: """ Get the last values from a nested dictionary. """ for key, item in dictionary.items(): if isinstance(item, dict): get_last_value(item, values) else: values.append(item) return values def custom_exception_handler(exc, context): """ Custom exception handler. Format of exception: { "field": "Field name", "detail": "Error message" } """ response = exception_handler(exc, context) if response is not None: if isinstance(response.data, dict): field = next(iter(response.data)) error_message = get_last_value(response.data)[0][0] error_response = Response({ "field": field, "detail": error_message }, status=response.status_code) return error_response else: error_message = str(response.data) error_response = Response({ "field": "non_field_errors", "detail": error_message }, status=response.status_code) return error_response return response </code>
from rest_framework.views import exception_handler
from rest_framework.response import Response

def get_last_value(dictionary: dict, values = []) -> list[list[str]]:
    """
    Get the last values from a nested dictionary.
    """
    for key, item in dictionary.items():
        if isinstance(item, dict):
            get_last_value(item, values)
        else:
            values.append(item)
    return values


def custom_exception_handler(exc, context):
    """
    Custom exception handler.
    Format of exception:
    {
        "field": "Field name",
        "detail": "Error message"
    }
    """
    response = exception_handler(exc, context)
    if response is not None:
        if isinstance(response.data, dict):
            field = next(iter(response.data))
            error_message = get_last_value(response.data)[0][0]
            error_response = Response({
                "field": field,
                "detail": error_message
            }, status=response.status_code)
            return error_response
        else:
            error_message = str(response.data)
            error_response = Response({
                "field": "non_field_errors",
                "detail": error_message
            }, status=response.status_code)
            return error_response
    return response

Why is this happening and how can I solve this?

I found the answer. It was because the values parameter in the get_last_value function was not resetting between requests.

I changed it to the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def get_last_value(dictionary: dict) -> list[dict]:
"""
Get the last values from a nested dictionary.
"""
values = []
for key, item in dictionary.items():
if isinstance(item, dict):
values.extend(get_last_value(item))
else:
values.append({key: item})
return values
</code>
<code>def get_last_value(dictionary: dict) -> list[dict]: """ Get the last values from a nested dictionary. """ values = [] for key, item in dictionary.items(): if isinstance(item, dict): values.extend(get_last_value(item)) else: values.append({key: item}) return values </code>
def get_last_value(dictionary: dict) -> list[dict]:
    """
    Get the last values from a nested dictionary.
    """
    values = []
    for key, item in dictionary.items():
        if isinstance(item, dict):
            values.extend(get_last_value(item))
        else:
            values.append({key: item})
    return values

It works now. It took way too long to figure this out.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật