Adam
Adam

Reputation: 3665

Django Get All Users

I am just starting out with Django and I am messing around just trying to pull a full list of users from postgres.

I used the following code:

group = Group.objects.get(name="Admins")
usersList = group.user_set.all()

How could you pull all users? I don't want to have to pick or assign a group.

group = Group.objects.get() #Doesn't Work.
usersList = group.user_set.all()

Upvotes: 71

Views: 116204

Answers (6)

Walulya francis
Walulya francis

Reputation: 573

if your using Basic Token Authorization please use this but it return a dictionary

from django.contrib.auth.models import User
users = User.objects.values()

Upvotes: 0

alireza Talebi
alireza Talebi

Reputation: 21

You can use this code

from django.contrib.auth.models import User
users = User.object.all()

Upvotes: 2

Brandon Taylor
Brandon Taylor

Reputation: 34553

from django.contrib.auth import get_user_model
User = get_user_model()
users = User.objects.all()

Upvotes: 137

Anirban
Anirban

Reputation: 176

Try this:

from django.contrib.auth.models import User
all_users = User.objects.values()
print(all_users)
print(all_users[0]['username'])

all_users will contain all the attributes of the user. Based upon your requirement you can filter out the records.

Upvotes: 7

Roshan Bagdiya
Roshan Bagdiya

Reputation: 2178

from django.contrib.auth.models import User
userList =User.objects.values()

will return values of all user in list format from Django User table. User.objects.all() will returns object.

Upvotes: 5

Harish Verma
Harish Verma

Reputation: 556

Django get user it's also simple method to get all user ,create user ,change password ,etc

from django.contrib.auth import get_user_model
user = get_user_model()
user.objects.all()

Upvotes: 6

Related Questions