3
- Yes, DRF supports signals.
I don’t have a good answer for question 2, mainly because I think the best place to do this is in the post_save method of your class. That way, you are sure your object was created, you have access to the object and to the request and you don’t need to bother implementing signals (which would probably require another class just for email sending).
6
Old question for an old version of Django, but if anyone comes across it… It’s not necessary to use signals. Django now has the built-in function send_mail
. Here’s the documentation:
https://docs.djangoproject.com/en/1.8/topics/email/
So you would use that function to create and send an email. It can even accept an HTML template as the message argument and populate it w/ Django data, which is nice.
So you’d define a function that included this method, for example:
def email_client(request):
id = request.POST.get('id')
client = Client.objects.get(id=id)
msg_html = render_to_string('templates/email.html', {'client': client})
template_email_text = ''
return send_mail('Lelander work samples', template_email_text, 'test@emailsender.com', ['test@emailrecipient.com'], html_message=msg_html, fail_silently=False)
And include that function in your DRF API view in post
:
class ClientList(generics.ListCreateAPIView):
queryset = Client.objects.all()
serializer_class = ClientSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
email_client(request)
return self.create(request, *args, **kwargs)
- [Django]-What is the best system to run django on?
- [Django]-Can this result in a deadlock in django + postgres?
- [Django]-Django rest framework – Field level validation in serializer
- [Django]-How to stream a file in a request?
- [Django]-Django ModelMultipleChoiceField queryset/filter for objects already associated