title: Change structure of serialized object in Django Rest Framework (pull ID field outside of model content)
body: <p>Say, you have a model with name and e-mail and used Model Serializer to serialize your data with Django Rest Framework. The output would be something like this:</p>
  
  <pre><code>[{
      "id": "1",
      "name": "Alex",
      "email": "alex@host.com"
  }, {
      "id": "2",
      "name": "Henry",
      "email": "henry@host.com"
  }]
  </code></pre>
  
  <p>Is there a way to "pull" a unique field outside the rest of the model content to get this?:</p>
  
  <pre><code>{
      "1": {
          "name": "Alex",
          "email": "alex@host.com"
      },
      "2": {
          "name": "Henry",
          "email": "henry@host.com"
      }
  }
  </code></pre>
  
  <p>I am able to "externalize" the ID by overriding serializer's <code>to_representation()</code> method (code sample below) but I can't get rid of the rudimental wrappers - currently JRF will return everything as <code>list[]</code> of <code>dict(instance)</code> similar to this:</p>
  
  <pre><code>data = list
  for item in instance:
      list.append(item.as_dict())
  </code></pre>
  
  <p>I don't need this to be a <code>list</code> of <code>dicts</code>, I want them to do be dict of dict?</p>
  
  <pre><code>data = {}
  for item in instance:
      data[item.id] = item.as_dict()
  </code></pre>
  
  <p>here's my to_representation() code, I can only manipulate what's returned for individual instance, so it has to be a dict, but I need this dict to be merged, rather than stacked at the output when serializer.data is fully cooked.</p>
  
  <pre><code>def to_representation(self, instance):
      rep = OrderedDict()
      fields = self._readable_fields
      id_field = filter(lambda i: i.label == "ID", fields)[0]
      if id_field:
          id_atr = id_field.get_attribute(instance).__str__()
      else:
          raise Exception('cannot serialize models without ID field')
      rep[id_atr] = {}
      for field in fields:
          try:
              attribute = field.get_attribute(instance)
          except SkipField:
              continue
          if attribute is None:
              rep[id_atr][field.field_name] = None
          else:
              rep[id_atr][field.field_name] = field.to_representation(attribute)
      return rep
  </code></pre>
  
  <p>I was able to make it work by reformatting the original serializer output, but it's surely not a clean solution with a lot of performance impact:</p>
  
  <pre><code>def list(self, request, *args, **kwargs):
      _data = super(ChatMessageViewSet, self).list(request, *args, **kwargs)
      _resp = {}
      for item in _data.data:
          _id = item.get('id', None)
          if not _id:
              raise Exception('cannot serialize data without id field')
          _resp[_id] = {}
          for element in item:
              _resp[_id][element] = item[element]
      _data.data = _resp
      return _data
  </code></pre>
  
  <p>as per @zaphod100-10 answer, I have created a custom ListSerializer class:</p>
  
  <pre><code>class ChatMessageListSerializer(serializers.ListSerializer):
  
      def to_representation(self, data):
  
          iterable = data.all() if isinstance(data, models.Manager) else data
          _data = {}
          for item in iterable:
              _data[item.id] = self.child.to_representation(item)
          return _data
  </code></pre>
  
  <p>and it formats the data the way I need this:
  <a href="https://i.stack.imgur.com/9CyQr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9CyQr.png" alt="debug view screenshot"></a></p>
  
  <p>but my data does not survive 'ListModelMixin':</p>
  
  <pre><code>serializer = self.get_serializer(queryset, many=True)
          return Response(serializer.data)
  </code></pre>
  
  <p>it returns a list[] of just the entity ids: <code>[1L, 2L, 3L...]</code></p>