70

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()

6 Answers 6

136
from django.contrib.auth import get_user_model
User = get_user_model()
users = User.objects.all()
8
  • 2
    @Adam this is well documented, very strange you couldn't find it. docs.djangoproject.com/en/dev/topics/db/queries/… Commented Apr 17, 2014 at 17:32
  • docs.djangoproject.com/en/dev/ref/models/querysets/…
    – nik7
    Commented Apr 17, 2014 at 17:33
  • 1
    @brandon You know I read that, and didn't realize you could do that on essentially any object.
    – Adam
    Commented Apr 17, 2014 at 17:34
  • 3
    No worries. It's just a standard Django ORM call, which exists on the model manager that exists on every model, unless overidden. Commented Apr 17, 2014 at 19:44
  • 2
    Use django.contrib.auth.get_user_model() instead of User!!! Maybe you will using a custom user model one day and use User directly will make a coding hell.
    – Zz Tux
    Commented Nov 19, 2017 at 2:20
7

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.

0
6

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()
5
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.

2

You can use this code

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

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.