我有一个基本上是工作单的模型表格。视图中的save方法设置为在保存时发送电子邮件通知。该模型有几个从父UserProfile模型继承的字段:
class RemoteRequest(models.Model):
create_date = models.DateTimeField(auto_now_add=True)
requested_by = models.ForeignKey(UserProfile)
client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
job_number = models.CharField(max_length=8, unique=False, blank=False, null=False)
cell_number = models.CharField(max_length=4, unique=False, blank=True, null=True)
job_name = models.CharField(max_length=200, unique=False, blank=True, null=True)
print_request = models.ManyToManyField(PrintRequest, unique=False, blank=True, null=True)
interactive_request = models.ManyToManyField(InteractiveRequest, unique=False, blank=True, null=True)
due_date = models.DateTimeField(auto_now_add=False)
job_notes = models.TextField(max_length=500, unique=False, blank=True, null=True)
asset_location = models.CharField(max_length=300, unique=False, blank=True, null=True)
asset_upload = models.FileField(upload_to=request_file_name, blank=True, null=True, max_length=300)
pdf_upload = models.FileField(upload_to=request_file_name, blank=True, null=True, max_length=300)
*notify_internal = models.ManyToManyField(UserProfile, related_name='+', unique=False, blank=True, null=True)*
notify_external = models.ManyToManyField(FreelanceProfile, unique=False, blank=True, null=True)
completed_by = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
complete_date = models.DateTimeField(auto_now_add=False, blank=True)
asset_return = models.FileField(upload_to=return_file_name, blank=True, null=True, max_length=300)
pdf_return = models.FileField(upload_to=return_file_name, blank=True, null=True, max_length=300)
return_notes = models.TextField(max_length=500, unique=False, blank=True, null=True)
def __unicode__ (self):
return u'%s %s %s %s %s' % (self.client, self.job_number, '-', self.cell_number, self.job_name)
UserProfile模型有一个电子邮件字段。这就是我试图传递给视图中的收件人列表:
def remote_request(request):
if request.method=='POST':
form = RemoteRequestForm(request.POST)
if form.is_valid():
name = form.cleaned_data['job_name']
subject = form.cleaned_data['job_name']
message = form.cleaned_data['job_notes']
sender = form.cleaned_data['requested_by']
recipients = [form.cleaned_data['**notify_internal.userprofile.email**']]
from django.core.mail import send_mail
send_mail(subject, message, sender, recipients)
remoterq = form.save(commit=False)
remoterq.save()
form.save_m2m()
return render(request, 'remote_tmps/remoterequest.html', {'form': form,
})
else:
form = RemoteRequestForm()
return render(request, 'remote_tmps/remoterequest.html', {'form': form,})
不幸的是“notify_internal.userprofile.email”引发了一个关键错误,我对如何正确传递这些电子邮件收件人感到茫然。
答案 0 :(得分:0)
是什么让你认为传递带有python点表示法的字符串会在字典查找中做一些特别的事情?
您正在保存对象,因此请在保存后保存m2m()后拉出收件人。
recipients = [user.email for user in remoterq.notify_internal.all()]
查看ManyToManyField
文档,了解如何获取数据。在字段属性中公开了QuerySet
接口。