score:0

Not sure if this applies, but Postgres provides a lot of tools for formatting data before the return. For example, here's a simple example based on a table called "mockit" that I've got some sample data in:

SELECT row_to_json(mockit_row) AS mockit_objects
FROM
      (SELECT   first_name, last_name, email
       FROM     mockit
       WHERE    gender = 'Male'
       LIMIT 10) AS mockit_row;

What you get is one object per row, as a JSON object

{"first_name":"Farlay","last_name":"Creegan","email":"fcreegan0@github.io"}
{"first_name":"Holt","last_name":"Cicco","email":"hcicco1@scribd.com"}

etc.

If you want a single JSON object with an array of objects:

SELECT array_to_json(array_agg(row_to_json(mockit_row))) AS mockit_objects
FROM
      (SELECT   first_name, last_name, email
       FROM     mockit
         WHERE  gender = 'Male'
       LIMIT 10) AS mockit_row;

D3 (I miss working with D3) dehydrates things into arrays of objects, as I recall. So you can provide the data in the format, if it's easier. CSV and TSV are smaller wire formats, but I don't think (?) there's currently support for output results to that format within Postgres as a SQL call.

Postgres is crazy fast, so it's good to push processing up to it.

score:1

Solved it myself, basically what I did was in views.py return JsonResponse(serializers.serialize('json', myData), safe=False) Where myData = (model).objects.all()

Then all I had to do was parse the data in javascript.


Related Query