title: Django: Break query result into multiple lists of common elements
body: <p><strong>EDIT</strong>: Added all relevant models. Added clarification.</p>
  
  <p>I have to query a table called <strong>Data</strong>, but instead of having just 1 list returned, possibly have several lists returned from a query. Otherwise take the queryset result and break it into an array of objects with common <strong>SensorParameter</strong> fields.</p>
  
  <p>I have model <strong>Data</strong>:</p>
  
  <pre><code>class Data(models.Model):
      sensor_param = models.ForeignKey(SensorParameter, on_delete=models.CASCADE)
      time = models.DateTimeField(db_index=True)
      value = models.FloatField()
  </code></pre>
  
  <p>And model <strong>SensorParameter</strong> has links to parameters (temperature, vibration, etc) and a sensor instance (serial number, etc):</p>
  
  <pre><code>class SensorParameter(models.Model):
      sensor = models.ForeignKey(Sensor)
      parameter = models.ForeignKey(Parameter)
  </code></pre>
  
  <p>And model <strong>Parameter</strong> (a container for different types of parameters the different sensors can possibly measure):</p>
  
  <pre><code>class Parameter(models.Model):
      parameter_name          = models.CharField(max_length=50)
      parameter_unit          = models.CharField(max_length=20, blank=True)
  </code></pre>
  
  <p>And finally a <strong>Sensor</strong> model that ties all others together:</p>
  
  <pre><code>class Sensor(models.Model):
      name            = models.CharField(max_length=50, null=False, blank=True)
      serial_number   = models.CharField(max_length=50)
      sensor_type     = models.ForeignKey(SensorType)
  </code></pre>
  
  <p>I can query the <strong>Data</strong> table for a certain time range period from a certain <strong>Sensor</strong> by doing a double backwards lookup and ordering the resulting query the way I'd like to have my lists broken down, first by <strong>sensor_parameter</strong>, then by <strong>time</strong>:</p>
  
  <pre><code>sensor = Sensor.objects.get(serial_number=request["data_source"])
  range_period = [request['start'], request['end']]
  # This is the queryset I have now, though it's not set in stone
  data_query = Data.objects.filter(sensor_parameter__sensor=sensor, time__range=range_period).order_by('sensor_parameter', 'time')
  </code></pre>
  
  <p>This results in a list of all data points, for the time period requested, for the sensor requested, in <strong>one big list</strong>.</p>
  
  <p>How can I then break down the resulting list into several lists (or an array) of objects with common <strong>SensorParameter</strong> fields?</p>
  
  <p>Example from:</p>
  
  <pre><code>queryset_overall_result =
  [
      {data_point_1: sensor: X, sensor_parameter: temp, value: 188, time: 00:01}
      {data_point_2: sensor: X, sensor_parameter: vibration, value: 5, time: 00:01}
      {data_point_3: sensor: X, sensor_parameter: temp, value: 185, time: 00:02}
      {data_point_4: sensor: X, sensor_parameter: vibration, value: 6, time: 00:02}
  ]
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>list_temp =
  [
      {data_point_1: sensor: X, sensor_parameter: temp, value: 188, time: 00:01}
      {data_point_3: sensor: X, sensor_parameter: temp, value: 185, time: 00:02}
  ]
  
  list_vibration = 
  [
      {data_point_2: sensor: X, sensor_parameter: vibration, value: 5, time: 00:01}
      {data_point_4: sensor: X, sensor_parameter: vibration, value: 6, time: 00:02}
  ]
  </code></pre>
  
  <p>Is there a better way to query the Data table to give me <strong>several</strong> lists arranged by time while filtered to another table? (SensorParameter)</p>
  
  <p>Or having the overall queryset result, use python to break it down into multiple lists of common object elements? An array would be ideal, but not sure how to accomplish this.</p>
  
  <p>Ideal scenario:</p>
  
  <pre><code>array = 
  [
      [
          {data_point_1: sensor: X, sensor_parameter: temp, value: 188, time: 00:01}
          {data_point_3: sensor: X, sensor_parameter: temp, value: 185, time: 00:02}
      ]
  ,
     [
          {data_point_2: sensor: X, sensor_parameter: vibration, value: 5, time: 00:01}
          {data_point_4: sensor: X, sensor_parameter: vibration, value: 6, time: 00:02}
      ]
  ]
  </code></pre>
  
  <p>Constraints: I don't know how many or which <strong>SensorParameters</strong> a <strong>Sensor</strong> can measure, this can be anywhere from 4 to 45ish depending on the <strong>Sensor_sensor_type</strong>. I need to try to keep queries to a minimum as this query can possibly return a ton of data and could possibly happen many times in quick succession. The less DB hits the better.</p>