title: How can I structure my JSON response using the Django Rest Framework serializer?
body: <p>I have a database table:</p>
  
  <pre><code>servicenumber | meternumber | usagedatetime | usage
  11111         | 22222       | 2019-01-01    | 1.85
  11111         | 22222       | 2019-01-02    | 2.25
  11111         | 22222       | 2019-01-03    | 1.55
  11111         | 22222       | 2019-01-04    | 2.15
  11111         | 33333       | 2019-02-01    | 2.95
  11111         | 33333       | 2019-02-02    | 3.95
  11111         | 33333       | 2019-02-03    | 2.05
  11111         | 33333       | 2019-02-04    | 3.22
  </code></pre>
  
  <p>As you can see, a service number can be related to multiple meter numbers. Think of the service number as a unique identifier for a geographic location that doesn't change.</p>
  
  <p>And I have a Django model:</p>
  
  <pre><code>class MeterUsage(models.Model):
  
      objectid = models.IntegerField(
          db_column='OBJECTID', unique=True, primary_key=True)
  
      servicenumber = models.IntegerField(
          db_column='serviceNumber', blank=True, null=True)
  
      meternumber = models.IntegerField(
          db_column='meterNumber', blank=True, null=True)
  
      usagedatetime = models.DateTimeField(
          db_column='usageDateTime', blank=True, null=True)
  
      usage = models.DecimalField(
          max_digits=38, decimal_places=8, blank=True, null=True)
  </code></pre>
  
  <p>And I have a basic serializer:</p>
  
  <pre><code>class MeterUsageSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = MeterUsage
          fields = (
              'usagedatetime',
              'usage'
          )
  </code></pre>
  
  <p>And the current response is:</p>
  
  <pre><code>[
      {
          "usagedatetime": "2019-01-01",
          "usage": "1.85"
      },
      {
          "usagedatetime": "2019-01-02",
          "usage": "2.25"
      },
      {
          "usagedatetime": "2019-01-03",
          "usage": "1.55"
      },
  
      ....
  ]
  </code></pre>
  
  <p>But what I really want is (need the usage separated by meter number):</p>
  
  <pre><code>[
    {
      "servicenumber": "11111",
      "meternumber": "22222",
      "usagedata": [
        {
          "usagedatetime": "2019-01-01",
          "usage": "1.85"
        },
        {
          "usagedatetime": "2019-01-02",
          "usage": "2.25"
        },
        {
          "usagedatetime": "2019-01-03",
          "usage": "1.55"
        },
        {
          "usagedatetime": "2019-01-04",
          "usage": "2.15"
        },
  
        ...
      ]
    },
    {
      "servicenumber": "11111",
      "meternumber": "33333",
      "usagedata": [
        {
          "usagedatetime": "2019-02-01",
          "usage": "2.95"
        },
        {
          "usagedatetime": "2019-02-02",
          "usage": "3.95"
        },
        {
          "usagedatetime": "2019-02-03",
          "usage": "2.05"
        },
        {
          "usagedatetime": "2019-02-04",
          "usage": "3.22"
        },
  
        ...
      ]
    },
  
    ...
  ]
  </code></pre>
  
  <p>My end goal is to display this data in a line chart using the ChartJS library in an Angular 7 app, and the data needs to be in this format:</p>
  
  <pre><code>  chartData = [
      {
        label: '22222',
        data: [1.85, 2.25, 1.55, 2.15] 
      },
      { 
        label: '33333',
        data: [2.95, 3.95, 2.05, 3.22]
      }
    ];
  </code></pre>
  
  <p>Is it possible to use a serializer to format the data as I've shown above? I've tried different techniques based on various tutorials I've read, but nothing seems to work and now I'm just confused about how to handle it.</p>
  
  <p>Any insight is appreciated!</p>