How to select a single field from database with django QuerySet API?

22,192 views
Skip to first unread message

marco sedda

unread,
Feb 26, 2009, 5:23:44 AM2/26/09
to Django users
Hi,
I want to select a "single" field from a table, i've read the
QuerySet API reference but i can't find anything to solve my query.

Just to explain:

If i've a table like:

User
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)

how i can execute a sql query "SELECT first_name FROM User" with
QuerySet API?

Russell Keith-Magee

unread,
Feb 26, 2009, 5:25:42 AM2/26/09
to django...@googlegroups.com

Depending on the exact format of the output that you want, either use values():

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

or values_list():

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

Yours,
Russ Magee %-)

Eric Abrahamsen

unread,
Feb 26, 2009, 5:28:39 AM2/26/09
to django...@googlegroups.com
You want the values() queryset call (http://docs.djangoproject.com/en/dev/ref/models/querysets/#values-fields
). Your query would probably look like
User.objects.values('first_name'), or if you wanted a plain list
(values usually returns a dictionary), you can use values_list() with
the 'flat' parameter.

Hope that helps,
Eric

>
> >

marco sedda

unread,
Feb 26, 2009, 5:55:38 AM2/26/09
to Django users
Thanks!!!
Reply all
Reply to author
Forward
0 new messages