title: How can I change part of a Django REST Framework JSON response from a list to a dictionary?
body: <p>More specifically, I need a nested part of my response to be a multi-item dictionary rather than a list of many single-item dictionaries.</p>
  
  <p>I am returning all the data I want, but I cannot figure out how to format it more sensibly without what I consider to be an extraneous list</p>
  
  <p>Here is the (simplified) response I am currently getting:</p>
  
  <pre><code>{
      "uielements": [
          {
              "home-bg": {
                  "label_text_color": "#123456",
                  "tag_display_text": "young"
              }    
          },
          {
              "home-speak-btn": {
                  "label_text_color": "",
                  "tag_display_text": null
              }
          }
      ]
  }
  </code></pre>
  
  <p>I would like the response to be in this format:</p>
  
  <pre><code>{
      "uielements": {
          "home-bg": {
              "label_text_color": "#123456",
              "tag_display_text": "young"
          },   
          "home-speak-btn": {
              "label_text_color": "",
              "tag_display_text": null
          }
      }
  }
  </code></pre>
  
  <p>Here is my relevant <code>serializer.py</code> code:</p>
  
  <pre><code>class UIElementProjectSerializer(serializers.ModelSerializer):
      class Meta:
          model = UIElement
  
      def to_representation(self, obj):
          result = super(UIElementProjectSerializer, self).to_representation(obj)
          uien = UIElementName.objects.filter(id=result['uielementname'])[0]
          return {uien.name: result}
  </code></pre>
  
  <p>and my relevant <code>views.py</code> code:</p>
  
  <pre><code>class ProjectViewSet(viewsets.ViewSet):
  
  ...
  
      @detail_route(methods=['get'])
      def uielements(self, request, pk=None):
          uielements = UIElementFilterSet(params)
          serializer = serializers.UIElementProjectSerializer(uielements, many=True)
          return Response({"uielements": serializer.data})
  </code></pre>