Keith John Hutchison
Keith John Hutchison

Reputation: 5277

columns from django.db.models.query.QuerySet

With a raw query the columns for the SQL that will be executed can be accessed like this.

    query_set = Model.objects.raw('select * from table')
    query_set.columns 

There is no columns attribute for django.db.models.query.QuerySet.

I'm not using raw ... I was debugging a normal query set using Q filters ... it was returning way too many records ... so I wrote code to see what the results would be with normal SQL. I'd like the see the columns and the actual SQL being called on a query set so I can diagnose what the issue is without guessing.

How do you get the columns or the SQL that will be executed from a django.db.models.query.QuerySet instance?

Upvotes: 0

Views: 248

Answers (1)

markwalker_
markwalker_

Reputation: 12849

Ok, so based on your comment, if you want to see what django has created in terms of SQL there's an attribute you can use on the Queryset.

Once you've written your query, qs = MyModel.objects.all(), you can then inspect that by doing qs.query, which if you print that out will show you the SQL query itself.

You'll have to inspect this query to see what columns are being included.

The query object is a class called Query which I can't find mention of in the django docs, but it's source is here; https://github.com/django/django/blob/master/django/db/models/sql/query.py#L136

Upvotes: 1

Related Questions