query.get_meta().fields == query.model._meta.fields, i.e. all the model fields
As far as I understand from reading, deferred_loading is populated only when the queryset is deferred itself, but at the same time if I use .values(*some_fields) instead of .only(*some_fields), its empty again (i.e. its not working for queryset generated by .values()). However I kept digging and I think I found something! compiler.get_select() Returns three values: - a list of 3-tuples of (expression, (sql, params), alias) - a klass_info structure, - a dictionary of annotations where this can be obtained by: queryset.query.get_compiler(using=queryset.db).get_select() >From what I have mange to test so farm its consistent against different types of querysets (deferred, values, +annotations, etc). Thank you for the help so far Matthew. On Thursday, June 15, 2017 at 10:12:26 PM UTC+3, Matthew Pava wrote: > > I have done some digging into the QuerySet API, and I think I may have > found what you are looking for: > MyModel.objects.all().query.get_meta().fields > > You can access the fields that are deferred by using > MyModel.objects.all().query.deferred_loading. Note that the deferred > columns will still be retrieved from the database when requested. > > You can also access annotations by using > MyModel.objects.all().query.annotations, which is an OrderedDict. > > In short, it doesn't look like there is an easy to way to simply access > all the fields that are going to appear in your SELECT clause of your query > from the query itself. You will need to process them. > > And, you're right; selecting the first item in a queryset does seem > hackish. You could even try this: > MyModel.objects.values().first() > which will return None if the queryset is empty, which doesn't have an > attribute of .keys(). > > > -----Original Message----- > From: [email protected] <javascript:> [mailto: > [email protected] <javascript:>] On Behalf Of Todor Velichkov > Sent: Thursday, June 15, 2017 12:36 PM > To: Django users > Subject: RE: How to get a list of queryset selected fields > > Hello, Matthew Pava, thank you for your answer. > This is probably the solution which I'm going to use if there isn't > anything better, but overall I don't like this approach for a few reasons: > > 1) Dict keys are unordered, I would prefer to be ordered as requested (or > as defined in the Model) I guess with some help from _meta.fields I can > partially solve this) > 2) . values() changes the queryset, I'm not sure if this won't have some > side effects of the final result? > 3) The whole approach of evaluating the queryset + picking the first item > is just looking hacky. But as I already said If there is nothing better I > will go for it. > > -- > 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 [email protected] <javascript:>. > To post to this group, send email to [email protected] > <javascript:>. > 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/7d3ecf53-ccad-4620-b34b-48c1788dba6a%40googlegroups.com. > > > 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 [email protected]. To post to this group, send email to [email protected]. 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/d3ca2933-9a82-461d-84be-ddc188cc6d94%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

