djangodjango-querysetintrospectioninspection

Django: inspect queryset to get applied filters


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...


Solution

  • 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