Home > Back-end >  How can we get all usernames list only not user objects with django ORM
How can we get all usernames list only not user objects with django ORM

Time:02-08

I want to know that how can we get list of required field data from django models. In my case i want to get all usernames from users models.

receivers = User.objects.all()

this return all users objects. I want to get usernames directly. Something like below:

receivers = User.objects.all(username)
output:
["ahmed","dummy","hamza","sentence"]

in sql query looks like below:

SELECT username from USER

CodePudding user response:

receivers=User.objects.values_list('username', flat=True)

Try this code, you can read more about this in django offical documents

CodePudding user response:

receivers = User.objects.all().values_list('username')

It will return list of all usernames.

  •  Tags:  
  • Related