How to get names of columns from QuerySet without executing it

84 views
Skip to first unread message

Eugene Kulak

unread,
Jul 4, 2020, 8:11:41 PM7/4/20
to Django users
How to get names of columns from QuerySet without executing it?
If it is not possible what would be the most easier way to execute a lighter version of the query. 
The query itself is unknown, it could be anything.

Akinfolarin Stephen

unread,
Jul 4, 2020, 8:30:40 PM7/4/20
to django...@googlegroups.com
you can filter out the column you want by using 
[name of model].objects.get(filter=[name of the column you want])

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/60c63ddd-80fa-44c5-9a8e-10091e307099o%40googlegroups.com.

Vishesh Mangla

unread,
Jul 4, 2020, 9:08:04 PM7/4/20
to django...@googlegroups.com
I think learning some sql commands would be good. Lots of youtube tuts available online. https://docs.djangoproject.com/en/3.0/topics/db/sql/#executing-custom-sql-directly

Eugene Kulak

unread,
Jul 4, 2020, 11:15:51 PM7/4/20
to Django users
Thank you, Stephen, but this is not what I asked. I can't change QuerySet and I don't know how it looks like.


On Saturday, July 4, 2020 at 4:30:40 PM UTC-4, Akinfolarin Stephen wrote:
you can filter out the column you want by using 
[name of model].objects.get(filter=[name of the column you want])

On Sun, Jul 5, 2020 at 10:12 AM Eugene Kulak <widowmak...@gmail.com> wrote:
How to get names of columns from QuerySet without executing it?
If it is not possible what would be the most easier way to execute a lighter version of the query. 
The query itself is unknown, it could be anything.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Eugene Kulak

unread,
Jul 4, 2020, 11:18:26 PM7/4/20
to Django users
I think reading the question first and answer only when you have anything to say is even better?


On Saturday, July 4, 2020 at 5:08:04 PM UTC-4, Vishesh Mangla wrote:
I think learning some sql commands would be good. Lots of youtube tuts available online. https://docs.djangoproject.com/en/3.0/topics/db/sql/#executing-custom-sql-directly

On Sun, Jul 5, 2020 at 1:59 AM Akinfolarin Stephen <akinfolar...@gmail.com> wrote:
you can filter out the column you want by using 
[name of model].objects.get(filter=[name of the column you want])

On Sun, Jul 5, 2020 at 10:12 AM Eugene Kulak <widowmak...@gmail.com> wrote:
How to get names of columns from QuerySet without executing it?
If it is not possible what would be the most easier way to execute a lighter version of the query. 
The query itself is unknown, it could be anything.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Eugene Kulak

unread,
Jul 5, 2020, 4:21:02 AM7/5/20
to Django users
The closes I can get with this is the following:

comp = queryset.query.get_compiler(DEFAULT_DB_ALIAS)
fields = [alias or sql.split('.')[1] for _, (sql, _), alias in comp.get_select()[0]]

Michael Fladischer

unread,
Jul 5, 2020, 6:23:03 PM7/5/20
to django...@googlegroups.com
What about introspecting the model associated with the QuerySet:

qs = SomeModel.objects.all()
columns = [f.column for f in qs.model._meta.fields]

Regards, Michael

Eugene Kulak

unread,
Jul 7, 2020, 12:36:23 AM7/7/20
to Django users
Michael, this will not work for cases with `values` and `annotations`

Dvil Djé

unread,
Jul 8, 2020, 3:56:51 PM7/8/20
to Django users
For a light version of the query, you can try a qs.first() as Django will limit the SQL request to the first element.
Reply all
Reply to author
Forward
0 new messages