首页 > 解决方案 > How to extract active user email address from Django

问题描述

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)

标签: pythondjango

解决方案


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)

推荐阅读