1313from django .http import Http404
1414from django .utils import encoding
1515from django .utils .translation import gettext_lazy as _
16- from rest_framework import exceptions
16+ from rest_framework import exceptions , relations
1717from rest_framework .exceptions import APIException
1818
1919from .settings import json_api_settings
@@ -368,6 +368,23 @@ def get_relation_instance(resource_instance, source, serializer):
368368 return True , relation_instance
369369
370370
371+ def get_serializer_from_context (context ):
372+ if hasattr (context ["view" ], "get_serializer" ):
373+ return context ["view" ].get_serializer ()
374+ elif hasattr (context ["view" ], "get_serializer_class" ):
375+ return context ["view" ].get_serializer_class ()()
376+ elif hasattr (context ["view" ], "serializer_class" ):
377+ return context ["view" ].serializer_class ()
378+
379+
380+ def get_relationship_fields (fields ):
381+ return {
382+ name : field
383+ for name , field in fields .items ()
384+ if isinstance (field , (relations .RelatedField , relations .ManyRelatedField ))
385+ }
386+
387+
371388class Hyperlink (str ):
372389 """
373390 A string like object that additionally has an associated name.
@@ -394,9 +411,14 @@ def format_drf_errors(response, context, exc):
394411 errors .extend (format_error_object (message , "/data" , response ))
395412 # handle all errors thrown from serializers
396413 else :
414+ serializer = get_serializer_from_context (context )
415+ fields = get_serializer_fields (serializer ) or dict ()
416+ relationship_fields = get_relationship_fields (fields )
417+
397418 for field , error in response .data .items ():
398419 field = format_field_name (field )
399- pointer = "/data/attributes/{}" .format (field )
420+ rel = "relationships" if field in relationship_fields else "attributes"
421+ pointer = "/data/{}/{}" .format (rel , field )
400422 if isinstance (exc , Http404 ) and isinstance (error , str ):
401423 # 404 errors don't have a pointer
402424 errors .extend (format_error_object (error , None , response ))
0 commit comments