我有一个来自django-registration的功能,它会将激活邮件重新发送给给定的收件人。我试图将该功能从接受给定电子邮件的多个用户转换为每个电子邮件只有一个用户。但是,当我尝试更改它时,它会抛出AttributeError
。
def resend_activation(self, email, site): # for multiple emails -- this works
sent = False
users = User.objects.all().filter(email=email)
if users:
for user in users:
registration_profiles = self.all().filter(user=user)
for registration_profile in registration_profiles:
if not registration_profile.activation_key_expired():
registration_profile.send_activation_email(site)
sent = True
return sent
def resend_activation(self, email, site): # for single email -- this does not work
sent = False
user = User.objects.all().filter(email=email)
if user:
registration_profile = self.all().get(user=user)
if not registration_profile.activation_key_expired():
registration_profile.send_activation_email(site)
sent = True
return sent
后一个函数抛出一个AttributeError
,但我无法弄清楚为什么没有for
循环函数不会“工作”。这似乎是我的问题?谢谢。
答案 0 :(得分:4)
尝试:
def resend_activation(self, email, site):
sent = False
# Get the user you are looking for
try:
single_user = User.objects.get(email=email)
except User.DoesNotExist:
return false
# Get all the profiles for that single user
registration_profiles = self.all().filter(user=user)
# Loop through, and send an email to each of the profiles belonging to that user
for registration_profile in registration_profiles:
if not registration_profile.activation_key_expired():
registration_profile.send_activation_email(site)
sent = True
return sent
在原文中,User.object.filter(email = email)返回 queryset ,它是从查询过滤器返回的数据库中的对象集合(email =电子邮件)即可。原始中的for循环循环遍历每个对象,并发送相应的电子邮件。您试图调用send _