title: How to serialize a foreign key relationship as key value pair django rest framework
body: <p>I'm trying to serialize an relationship in DRF whereas I attempt to get the following output:</p>
  <pre><code>&quot;statistics&quot;:[
     &quot;Attacks&quot;:{
        &quot;id&quot;:971,
        &quot;home_value&quot;:3,
        &quot;away_value&quot;:2,
        &quot;value&quot;:5
     },
     &quot;Corners&quot;:{
        &quot;id&quot;:972,
        &quot;home_value&quot;:0,
        &quot;away_value&quot;:0,
        &quot;value&quot;:0
     }
  ]
  </code></pre>
  <p>I wrote the following code for my serializer of this model:</p>
  <pre><code>class StatisticSerializer(serializers.ModelSerializer):
      class Meta: 
          model = Statistic
          fields = ['id', 'home_value', 'away_value', 'name', 'value']
  
      def to_representation(self, instance: Statistic):
          representation = {instance.name: {
              &quot;id&quot;: instance.id, 
              &quot;home_value&quot;: instance.home_value,
              &quot;away_value&quot;: instance.away_value, 
              &quot;value&quot;: instance.value
          }}
  
          return representation
  </code></pre>
  <p>Which gets me the following output:</p>
  <pre><code>&quot;statistics&quot;:[
     {
        &quot;Attacks&quot;:{
           &quot;id&quot;:971,
           &quot;home_value&quot;:3,
           &quot;away_value&quot;:2,
           &quot;value&quot;:5
        }
     },
     {
        &quot;Corners&quot;:{
           &quot;id&quot;:972,
           &quot;home_value&quot;:0,
           &quot;away_value&quot;:0,
           &quot;value&quot;:0
        }
     }
  ]
  </code></pre>
  <p>Is there any way to get it as demonstrated in the first example using django rest framework?</p>