Search code examples
djangodjango-usersdjango-email

How to get email address of all users in Django


I am learning django and I am stuck with this problem.

How do I get the email address of all the users in Django. The user can be of any type like superuser, general user etc.

I tried the following but I am not getting email address of all the users.

user = User.objects.get(id=2)
user_email = user. Email
print(user_email)

I want something like a list of all the email addresses. Can someone please help me with it?


Solution

  • email_list = list(User.objects.values_list("email", flat=True))
    

    Read more about values_list https://docs.djangoproject.com/en/4.1/ref/models/querysets/#values-list