I have a model with a field associated to a user with ForeignKey, I need to send an email to that user when the user do an action, my question is not about to send the email instead is how to get that email. App works like that: User pick one of a lot of options (clicking in a button) that button call the ID of the model that contains the user_id through a DetailView, and when that button it's clicked send the email. My view:
class NotificationEmail(DetailView):
model = Articles
template_name = 'email.html'
def dispatch(self, request, *args, **kwargs):
p = Articles.objects.filter(pk=self.kwargs['pk'])
for articles in p:
print (articles.user_id__email)
to = articles.user_id__email
html_content = '<p>This is your email content...</p>'
msg = EmailMultiAlternatives('You have an email',html_content,'from@server.com',[to])
msg.attach_alternative(html_content,'text/html')
msg.send()
return super(NotificationEmail, self).dispatch(request, *args, **kwargs)
Models:
class Articles(models.Model):
name = models.CharField(max_length=100)
user = models.ForeignKey(User)
My questions basically is how can I get the email of the user that has the ID of that model? Im trying with articles.user_id__email
I suppose that this is the way to look up the field, I know that is wrong, but.. How can I achieve this?