python - 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)
解决方案
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)
推荐阅读
- c# - Asp.Net MVC 5 FileResult 总是使用 ANSI 编码下载文件
- c# - 循环获取对象C#中的重复项
- java - 使用 JDBC 将大文件从 postgres 数据库流式传输到文件系统
- mysql - 如何在续集,节点js中的where条件下使用like
- android - 如何在 >= Android O 中设置通知通道默认值
- laravel - 删除软删除中的数据包括媒体文件
- css - 重新加载时的样式不一致(?)(nextjs项目)
- sql - 在 sqlite 中可以进行此查询吗?
- python-3.x - HBase 与多列的一对多关系
- c# - ASP.NET 4.6 MVC 如何返回包含正确时区的日期时间数据的 Json 结果?