title: Django queryset: Build a dictionary from a queryset, with common elements
body: <p>I'm trying to construct a dictionary from my database, that will separate my data into values with common time stamps.</p>
  
  <pre><code>data_point:
      time: &lt;timestamp&gt;
      value: integer
  </code></pre>
  
  <p>I have 66k data points, out of which around 7k share timestamps (meaning the measurement was taken at the same time.</p>
  
  <p>I need to make a dict that would look like:</p>
  
  <pre><code>{
    "data_array": [
      {
        "time": "2018-05-11T10:34:43.826Z",
        "values": [
          13560465,
          87856595,
          78629348
        ]
      },
      {
        "time": "2018-05-11T10:34:43.882Z",
        "values": [
          13560689,
          78237945,
          92378456
        ]
      }
    ]
  }
  </code></pre>
  
  <p>There are other keys in the dictionary, but I'm just having a bit of a struggle with this particular key.</p>
  
  <p>The idea is, look at my data queryset, and group up objects that <strong>share a timestamp</strong>, then add a key <strong>"time"</strong> to my dict, with the <strong>value</strong> being the <strong>timestamp</strong>, and an <strong>array "values"</strong> with the value being a list of those <strong>data.value</strong> objects</p>
  
  <p>I'm not experienced enough to build this without looping a lot and probably being very innefficient. Some kind of "while timestamp doesn't change: append value to list", though I'm not sure how to go about that either.</p>
  
  <p>Ideally, if I can do this with queries (should be faster, right?) I would prefer that.</p>