Debugging QuerySets in Django


When in debug mode and looking at a QuerySet that did return results, how / where can I see the objects in the debugger? I am not interested in the code to evaluate (e.g. Class.objects.all() etc, but more like the structure, e.g.:

- QuerySet
-- object_list
--- object[0]
--- object[1]
--- object[n]



Let’s say I am looking at a QuerySet in the debugger, I am getting a bunch of attributes and related objects, but I cannot seem to find the actual objects that I am after. If I have a class A and I am executing qs = A.objects.get.all() and I am looking at the structure of qs in the debugger, I am getting a bunch of different items but cannot find the actual objects / instances of A.

debug screenshot


From the documentation,

Internally, a QuerySet can be constructed, filtered, sliced, and generally passed around without actually hitting the database. No database activity actually occurs until you do something to evaluate the queryset.

Which means, wheever you call Class.objects.all(), it won’t fetch the actual instances from DB (just like generators in Python)

to evaluate all objects, do something like this,

all_instance = [i for i in Class.objects.all()]

The above method is not recomended for your production/live code, because it’s too ugly to store all instance in a list

Answered By – JPG

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published