tomchristie/django-rest-framework — pull request #2119 by g-cassie (2014-11-24 14:00:16+00:00)
Title: Use `ReturnDict` and `ReturnList` for nested serializers

This PR just contains some initial ideas for implementing this without moving the `ReturnDict`/`ReturnList` wrapping back into `.to_representation()`. That said a lot of tests are failing and I'm not sure this is the right path so wanted to seek guidance from the community before going to far ahead on this.

## Explanation

I am working on a set of plugins for DRF that will allow it to work out of the box with `Ember Data` (ED), a data persistence library that is commonly used with Ember.js. ED accepts two forms of serialized data, one of which it refers to as the ActiveModel style serialization.

ActiveModelJson expects a JSON payload that looks like this:

```
dealerships: [
{
id: 1,
title: 'The Car Depot',
car_ids: [1, 2, 3, 4],
}
...
],
```

Or like this:

```
dealership: [
id: 1,
title: 'The Car Depot',
cars: [ {
id: 1,
title: 'Toyata',
dealership_id: 1,
},
...
]
]
```

In order to convert to this format in a DRF serializer, the renderer must be able to check whether a particular field is a model relation and if so change it from `plural_words` to `plural_word_ids` or from `singular_word` to `singular_word_id`. Note that you cannot just check if the property contains an array or integer as that would result in converting integer fields and arbitrary lists by accident.

[This code](https://github.com/g-cassie/ember-drf/blob/master/ember_drf/utils.py#L31-L74) worked until about a week ago when the `ReturnDict`/`ReturnList` wrapping was moved into the `.data` property. Now that ReturnDict/Lists do not nest, I do not see any way of achieving the same behaviour using a renderer alone. At the same time I think the changes I am trying to make are the kind of purely aesthetic changes which should be implemented in the Renderer.

This is a big blocker for my project so anything I can do to move this along quickly would be greatly appreciated.