Issue
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.
Solution
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
Answered By - akotian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.