You may be looking for values.
MyModel.objects.all().values()
returns a QuerySet list of dictionaries with the field names as the keys.

You can specify which specific fields you want to return by passing them as 
arguments to values().
MyModel.objects.all().values('id', 'name')
Then you can use the first item in the QuerySet to retrieve all the keys.
MyModel.objects.all().values('id', 'name')[0].keys()


https://docs.djangoproject.com/en/1.11/ref/models/querysets/#values

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Todor Velichkov
Sent: Thursday, June 15, 2017 10:52 AM
To: Django users
Subject: How to get a list of queryset selected fields

That's it, I want to get a list of all fields which are going to be selected by 
a queryset, is it possible?

e.g.
qs = MyModel.objects.only('id', 'name')
select = qs.query.select #should be ['id','name']

PS. I notice there is a `qs.query.select`, but its empty. No idea if its the 
same as what I'm looking for, and how to use/fill it.
PS2. The method should return all fields, i.e. annotations too (if there are 
any).



--
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+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/56059101-4006-4ab4-91dd-3a696e07f101%40googlegroups.com<https://groups.google.com/d/msgid/django-users/56059101-4006-4ab4-91dd-3a696e07f101%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/702d37452161498a904138a766e0824c%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to