Django Get All Users

PythonDjango

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

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.

Solution 5 - Python

You can use this code

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdamView Question on Stackoverflow
Solution 1 - PythonBrandon TaylorView Answer on Stackoverflow
Solution 2 - PythonHarish VermaView Answer on Stackoverflow
Solution 3 - PythonAnirban BanerjeeView Answer on Stackoverflow
Solution 4 - PythonRoshan BagdiyaView Answer on Stackoverflow
Solution 5 - Pythonalireza TalebiView Answer on Stackoverflow