13

Is there a way to inspect a queryset and get info about which filters/exclude have been applied?

I need it for debugging: I cannot understand why my queryset excludes some data...

skaffman
  • 398,947
  • 96
  • 818
  • 769
Don
  • 16,928
  • 12
  • 63
  • 101
  • 3
    You can use the `QuerySet.query` method to see what the SQL looks like, if you haven't already. Maybe that would help. – Dan Breen Oct 27 '11 at 14:16

3 Answers3

13

That doesn't seem easy to do. Each filter is applied differently to the query object so you're not going to find a cleanly laid out "filter1", "filter2", "filter3".

Check out myqueryset.query.__dict__ - the incoming filter is separated into relevant areas immediately and no record stored. Details in django.db.models.sql.query.Query.

I'd check out the SQL instead.

print myqueryset.query 
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
3

You can use also:

your_qs.query.where.children

or:

your_qs._has_filters().__dict__['children']

and to access to the first filter that you applied:

your_qs._has_filters().__dict__['children'][0].__dict__
asmatrk
  • 237
  • 2
  • 9
2

If you are debugging in a shell:

from django.db import connection
print connection.queries

If you are making requests in a browser use django debug toolbar, it's a great tool and can be very helpful:

Django Debug Toolbar

eveiga
  • 206
  • 3
  • 9