Skip to content

Errors from parent-level validation of nested serializer fields break the browsable API. #4073

Closed

Description

@alexcreid

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

(See PR #4074 for a test that reproduces this.)

  1. Create a serialiser that contains a nested serialiser as one of its fields.
  2. Add validation for the nested serialiser field in the top-level serialiser (for example, as a validate_<field name> method.
  3. Instantiate the serialiser with data that will cause the validate_<field name> method to fail.
  4. Retrieve the NestedBoundField for the nested serialiser using the serialiser's form-like api (<serializer instance>[<field name>])
  5. Attempt to retrieve a BoundField for one of the nested serialiser's fields from the NestedBoundField instance.

Expected behaviour

A BoundField is returned.

Actual behaviour

An AttributeError is raised, as the NestedBoundField attempts to treat its errors as a dictionary, even though they are the list of error messages from the parent serialiser's validation error. This impacts the browsable API.

(It's not entirely clear to me what the behaviour should be where a nested serialiser has a list of errors rather than a dictionary, but it probably shouldn't be to error out.)

Activity

  1. lovelydinosaur commented on Jun 1, 2016

    @lovelydinosaur
    Member

    Confirmed - thanks for raising the issue.

    Using the following...

    class Nested(serializers.Serializer):
        b = serializers.IntegerField()
        c = serializers.IntegerField()
    
    
    class ExampleSerializer(serializers.Serializer):
        a = Nested()
    
        def validate_a(self, data):
            raise serializers.ValidationError('Oh no')
    
    class ExampleView(GenericAPIView):
        serializer_class = ExampleSerializer
    
        def get(self, request):
            return Response()
    
        def post(self, request):
            serializer = self.get_serializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            return Response()
    

    Submitting the form results in...

    image

  2. added this to the 3.4.0 Release milestone on Jun 1, 2016
  3. changed the title [-]Errors from parent-level validation of nested serializer fields breaks the form-like API[/-] [+]Errors from parent-level validation of nested serializer fields break the browsable API.[/+] on Jun 1, 2016
  4. 58 remaining items

  5. carltongibson commented on Oct 5, 2017

    @carltongibson
    Collaborator

    I'm going to de-milestone this for now.

  6. removed this from the 3.7.0 Release milestone on Oct 5, 2017
  7. stale commented on Oct 19, 2025

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  8. Shrikantgiri25 commented on Nov 28, 2025

    @Shrikantgiri25
    Contributor

    Hi @auvipy,
    I’ve been looking into this issue and noticed that NestedBoundField.__getitem__() receives self.errors as a list when parent-level validation fails. This causes child fields to have errors=None, which breaks the browsable API.
    As a minimal fix, I’m considering normalizing list-type errors to an empty dict:

    if isinstance(self.errors, list):
        error = {}

    Before working on this, is this the correct direction for the fix?
    Thanks!

  9. browniebroke commented on Feb 24, 2026

    @browniebroke
    Collaborator

    Just tested again that example and it works now:

    Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions