我想向拥有顾问权限的所有用户发送电子邮件: perms.profiles.consultant_permission
我的代码如下:
model.py
class Profile(models.Model):
//some attributes
class Meta:
permissions = (
('consultant_permission', 'Consultant Permission'),
)
view.py
consultant_msg_html = render_to_string('email/consultant_review_email.html', context)
send_mail("Register sent to review",
consultant_msg_html,
registration.consultant.email,
[registration.consultant.email,],
html_message=consultant_msg_html,
fail_silently = False)
我怎样才能做到这一点?
答案 0 :(得分:6)
首先,您需要获得具有特定权限的所有用户:
from django.contrib.auth.models import User, Permission
from django.db.models import Q
perm = Permission.objects.get(codename='consultant_permission')
users = User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm) ).distinct()
recipients = list(i for i in users.values_list('email', flat=True) if bool(i))
然后,您可以在send_email()
中使用recipients
。