Django 将函数从多个转换为单个
我有一个来自django registration的功能,它向给定的收件人重新发送激活电子邮件。我正在尝试将该功能从接受给定电子邮件的多个用户转换为每个电子邮件仅接受一个用户。但是,当我尝试更改它时,它抛出了一个Django 将函数从多个转换为单个,django,django-registration,Django,Django Registration,我有一个来自django registration的功能,它向给定的收件人重新发送激活电子邮件。我正在尝试将该功能从接受给定电子邮件的多个用户转换为每个电子邮件仅接受一个用户。但是,当我尝试更改它时,它抛出了一个AttributeError def resend_activation(self, email, site): # for multiple emails -- this works sent = False users = User.objects.all().f
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
循环,该函数就不能“工作”。我这里有什么问题?谢谢。试试:
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,它是从数据库中从查询filter(email=email)返回的对象的集合。原始文件中的for循环在这些对象中循环,并发送相应的电子邮件。您正在尝试呼叫发送\uuu\p>请尝试:
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,它是从数据库中从查询filter(email=email)返回的对象的集合。原始文件中的for循环在这些对象中循环,并发送相应的电子邮件。你想打电话给送信人_