title: Format Django QuerySet to output values as an array
body: <p>I have data in a table that look like this:</p>
  
  <pre><code>src_id, dst_id, params
  int   , int   , array
  </code></pre>
  
  <p>I'm querying the data to extract some values from the array
  with the following Django query</p>
  
  <p><code>dataset = query_set.values_list('src_id', 'dst_id', *[e.field for e in settings])</code></p>
  
  <p>I need to output the data like this:</p>
  
  <pre><code>[
  [1,2,[a,b,c,d]],
  [3,4,[a,c,d,e]],
  ...
  ]
  </code></pre>
  
  <p>but the <code>values_list</code> returns a list of tuples like 
  <code>(1,2,a,b,c,d)(3,4,a,c,d,e)...</code></p>
  
  <p>So I've been doing the following to recombine the tuple values into an array:</p>
  
  <pre><code>[[d[0], d[1], d[2:]] for d in dataset]
  </code></pre>
  
  <p>It works fine, but I have 1M+ rows, and it is slow. It is also using a lot of memory. Ideally I want to format the data properly in the SQL (through Django) and push the output directly to the JSON serializer without having to generate an intermediate array.</p>
  
  <p>Is there a way to optimize this code? </p>
  
  <p>I looked at using a loop, or lambda but that doesn't make much of a difference. I looked at using <code>array</code> but it only takes primitive types, so no much luck with array of arrays.</p>
  
  <p>I am looking for a way to query the DB and output the data directly in the right format, if possible in Django:</p>
  
  <p>I see Django can do slice array lookup like </p>
  
  <p><code>dataset = query_set.values_list('src_id', 'dst_id', 'data__3_10')</code></p>
  
  <p>to get the values between indices 3 and 10 for example, but I need to retrieve specific indices (like 1,4,5,6,8,11 for example), not slices.</p>
  
  <p>Any hint on how to format the output of a query like this? 
  is it possible, or just too esoteric for Django?
  Do I need <code>raw</code> SQL for this?</p>
  
  <p>Thanks</p>