Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell which fields have been deferred/only'd in a Django queryset

I am trying to serialize a queryset into json using my custom iterator. On the models I detect the fields in the model and insert them into the JSON dict as I need them.

I am having trouble figuring out how to determine which fields have been deferred in the model using the defer or only queryset function.

Is there a way, and how, to find out which fields are deferred and how to skip over them?

like image 725
Xealot Avatar asked Dec 24 '10 01:12

Xealot


2 Answers

Current version of Django (1.9) has a method on all model instances: instance.get_deferred_fields()

This seems to be the "official" implementation of Alexey's answer.

like image 117
SMX Avatar answered Oct 01 '22 03:10

SMX


Somewhat buried...

queryset.query.get_loaded_field_names()

like image 29
Xealot Avatar answered Oct 01 '22 03:10

Xealot