I want to retrieve a list of all of the values for one field from a query in django. For example, I have a query of users, but rather than a queryset (or list) of user objects, I want a list just the usernames (strings). In a sense this is asking to restrict only to one column of data.
6 Answers
Have you tried
list(User.objects.all().values_list('username', flat=True))
If you only pass in a single field, you can also pass in the flat parameter. If True, this will mean the returned results are single values, rather than one-tuples. Additionally, casting it to a list makes the returned value a list instead of a queryset
-
1It actually returns a QuerySet object not a list and hence does not support item assignment.– AyushOct 25, 2017 at 11:12
-
Something funny happened on my end. I wanted to get a list of all the fields from something that wasn't the
User
object. I put it in and got adjango.core.exceptions.FieldError: Cannot resolve keyword '' into field. Choices are:
error but still showed all of the different fields for that model.... I'll take what I can get. May 18, 2018 at 16:33
To get the list of usernames:
>>> User.objects.all().values('username')
>>> [{'username': u'u1'}, {'username': u'u2'}]
>>> User.objects.all().values_list('username')
>>> [(u'u1',), (u'u2',)]
If you want just strings, a list comprehension can do the trick:
>>> usr_names = User.objects.all().values('username')
>>> [u['username'] for u in usr_names]
>>> [u'u1', u'u2']
Using values_list
:
>>> usr_names = User.objects.all().values_list('username')
>>> [u[0] for u in usr_names]
>>> [u'u1', u'u2']
-
2Close; but this gives me a list of dictionaries (actually a ValuesQuerySet, but...) If I use values_list I get a list of tuples which is closer still, but I'd really like a straight list e.g. [u'u1', u'u2'] Feb 1, 2013 at 3:20
-
You're right. I edited the answer. I prefer values() btw, don't know why, just a preference. Hope it helps. Feb 1, 2013 at 3:30
-
1Can't you simply cast the output of values_list to list() rather than using the comprehension? As in
usr_names = list(User.objects.all().values_list('username',flat=True))
Feb 12, 2020 at 15:08
As you wrote:
"but rather than a queryset (or list) of user objects"
Soulution above still queryset
usr_names = User.objects.all().values_list('username')
This solution:
usr_names = [str(elem) for elem in list(User.objects.all().values_list('username'))]
It will return a list of strings
The simplest way you can get the list of objects of an attribute is to first get a query-set of that attribute alone using values_list
then converting the django query-set to a python set using set()
and finally to a list using list()
.
In your scenario:
user_names = list(set(User.objects.all().values_list('username', flat=True)))
You replace 'field_name' with your own
users_name = [i.field_name for i in Users.objects.all()]
To get the list and easily understands it since value list returns tuples in a list
users= User.objects.all().values_list("username")
usernames= []
for item in categories:
usernames.append(item[0])