Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Send Data to View Without Serializer

I have a Magic model in my application. I need the user who is attached to this model to go to their email address to verify something before they can access the information. This should happen over a RESTful API. The problem is, the user should not necessarily be logged in to access this feature (for design decisions I had no control over).

I have implemented the logic for generating the necessary information and sending the email (only if such an email is registered), authentication once the emailed information is accessed, etc.

My question: How do I implement a view that takes a user email in the body (or url)?

Approach 1: create a url /magic_api/v1/tdbverification/(?P<email>[\w.@]+)/ and extract the email address and send the email.

Problem with 1: I cannot seem to extract the email and I need to return a json object without having a serializer

Approach 2: Create a view that takes body: {"email": "<[email protected]>"}

Problem with 2: This requires a serializer without a model (as this view is just for sending the email, it does not change model objects). I tried working with a serializers.Serializer class but could not figure out how to incorporate the email sending logic.

Any help with this will be much appreciated.

like image 890
dot64dot Avatar asked Dec 23 '22 03:12

dot64dot


1 Answers

Why you always need a serializer! You can write a view without using a serializer. Example

from rest_framework.views import APIView
from rest_framework.response import Response

class Test(APIView):
    def post(self, request):
        email = request.data['email']
        ...  your logic ...
        return Response(...)
like image 66
a_k_v Avatar answered Apr 06 '23 11:04

a_k_v