#32226: QuerySet.explain(format='json') outputs repr'd JSON on PostgreSQL
-------------------------------------+-------------------------------------
               Reporter:  Adam       |          Owner:  nobody
  (Chainz) Johnson                   |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Call `.explain(format='json')` on a `QuerySet` on PostgreSQL, and the
 result *looks* like JSON, but isn't:

 {{{
 In [5]: User.objects.filter(is_staff=True).explain(format='json')
 Out[5]: "[{'Plan': {'Node Type': 'Seq Scan', 'Parallel Aware': False,
 'Relation Name': 'auth_user', 'Alias': 'auth_user', 'Startup Cost': 0.0,
 'Total Cost': 478.12, 'Plan Rows': 23, 'Plan Width': 157, 'Filter':
 'is_staff'}}]"
 }}}

 One needs to use `ast.literal_eval` and `json.dumps` to get real JSON text
 that can be pasted into tools like [https://tatiyants.com/pev/ PostgreSQL
 explain viewer]:

 {{{
 In [10]: explain =
 User.objects.filter(is_staff=True).explain(format='json') ;
 print(json.dumps(ast.literal_eval(explain)))
 [{"Plan": {"Node Type": "Seq Scan", "Parallel Aware": false, "Relation
 Name": "auth_user", "Alias": "auth_user", "Startup Cost": 0.0, "Total
 Cost": 478.12, "Plan Rows": 23, "Plan Width": 157, "Filter": "is_staff"}}]
 }}}

 I guess this is because `psycopg2` loads the JSON as a Python list of
 dicts, then `SQLCompiler.explain_query` does `yield ' '.join(str(c) for c
 in row)`, which turns that list into its `repr()`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32226>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.7e2c7acc2bc47135b2dacc75e31c790f%40djangoproject.com.

Reply via email to