[Django]-How to fetch only specific columns of a table in django?

46👍

QuerySet.only() and QuerySet.defer() can be used to refine which fields the ORM will pull, deferring the others until the appropriate attributes on the models are accessed.

43👍

With django 1.8:
use the values_list

Entry.objects.values_list('id', 'headline')

https://docs.djangoproject.com/en/1.8/ref/models/querysets/

👤max

6👍

if you need just the values as a dictionary use objects.values(”). Its also faster.

see docs: http://docs.djangoproject.com/en/dev/ref/models/querysets/#values-fields

Leave a comment