1

I want to extract a list of active users email addresses from Django's Home › Authentication and Authorization › Users page and put it into a template. Can someone help me achieve this?

So far I'm trying to do something like this:

from django.contrib.auth.models import User

if User.is_active:
    emails = User.objects.get(email=request.user.email)

3 Answers3

10

Here's how to get a list of email addresses for active users:

emails = User.objects.filter(is_active=True).values_list('email', flat=True)

If you want to exclude empty email addresses, you can do it like this:

emails = User.objects.filter(is_active=True).exclude(email='').values_list('email', flat=True)
Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25
1

You can select email only from active users using this query.

from django.contrib.auth.models import User

emails = User.objects.filter(is_active=True).values_list('email', flat=True)
Adriano Silva
  • 2,518
  • 1
  • 17
  • 29
0

One solution :

email_list = User.objects.filter(is_active=True).values_list("email", flat=True)

See the doc on values_list for more information.

stellasia
  • 5,372
  • 4
  • 23
  • 43