How to get email address of all users in Django

660 Views Asked by At

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?

1

There are 1 best solutions below

0
lucutzu33 On BEST ANSWER
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