Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django models | get specific columns

Tags:

django

Is there a way to filter and get only specific columns?

For example, get all entries with the column first_name.

like image 775
Switch Avatar asked Apr 19 '10 15:04

Switch


People also ask

What are QuerySets in django?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.


2 Answers

QuerySet.values() or QuerySet.values_list(), e.g.:

Entry.objects.values('first_name') 
like image 129
Ignacio Vazquez-Abrams Avatar answered Nov 05 '22 06:11

Ignacio Vazquez-Abrams


If you want a list of only the values, use:

Entry.objects.values_list('first_name', flat=True) 
like image 42
Daniel Nicolae Avatar answered Nov 05 '22 07:11

Daniel Nicolae