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.

  • Possible duplicate of [django-rest-framework 3.0 create or update in nested serializer](http://stackoverflow.com/questions/27434593/django-rest-framework-3-0-create-or-update-in-nested-serializer) – Sardorbek Imomaliev Nov 24 '16 at 12:34

1 Answers1

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.

Evans Murithi
  • 3,197
  • 1
  • 21
  • 26