1

I would like to display nested entities in the Django REST response – not hyperlinked entities or primary keys - the actual entity inside the parent.

This would look something like this:

{ 'id': 5
  'name' : 'blah'
  'children' : [
      {'id' : 77, 'foo' : 'bar'},
      {'id' : 78, 'foo' : 'bar'},
      ...
  ]
}

This is mentioned in the REST documentation as one possible way of representing relationships between entities, but the documentation doesn't indicate how it can be done.

1

1 Answer 1

1

The documentation indicates use of nested relationships: DRF Nested relationships

Basically you put the related_name of the child model in the serializer e.g. related_name = ChildSerializer(many=True, read_only=True). The link has a pretty good example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.