Python Django Rest Post API without storage
<p>I would like to create a web api with Python and the Django Rest framework. The tutorials that I have read so far incorporate models and serializers to process and store data. I was wondering if there's a simpler way to process data that is post-ed to my api and then return a JSON response without storing any data.</p>

<p>Currently, this is my urls.py</p>

<pre><code>from django.conf.urls import url
from rest_framework import routers
from core.views import StudentViewSet, UniversityViewSet, TestViewSet

router = routers.DefaultRouter()
router.register(r'students', StudentViewSet)
router.register(r'universities', UniversityViewSet)
router.register(r'other', TestViewSet,"other")

urlpatterns = router.urls
</code></pre>

<p>and this is my views.py</p>

<pre><code>from rest_framework import viewsets
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import University, Student
from .serializers import UniversitySerializer, StudentSerializer

import json
from django.http import HttpResponse

class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class UniversityViewSet(viewsets.ModelViewSet):
    queryset = University.objects.all()
    serializer_class = UniversitySerializer

class TestViewSet(viewsets.ModelViewSet):    
    def retrieve(self, request, *args, **kwargs):
        return Response({'something': 'my custom JSON'})
</code></pre>

<p>The first two parts regarding Students and Universities were created after following a tutorial on Django setup. I don't need the functionality that it provides for creating, editing and removing objects. I tried playing around with the TestViewSet which I created.</p>

<p>I am currently stuck trying to receive JSON data that gets posted to the url ending with "other" and processing that JSON before responding with some custom JSON.</p>

<p><strong>Edit</strong></p>

<p>These two links were helpful in addition to the solution provided:</p>

<p><a href="http://stackoverflow.com/questions/13603027/django-rest-framework-non-model-serializer">Django REST framework: non-model serializer</a></p>

<p><a href="http://jsatt.com/blog/abusing-django-rest-framework-part-1-non-model-endpoints/" rel="nofollow">http://jsatt.com/blog/abusing-django-rest-framework-part-1-non-model-endpoints/</a></p>

<p>You can use their generic <a href="http://www.django-rest-framework.org/api-guide/views/" rel="nofollow">APIView</a> class (which doesn't have any attachment to Models or Serializers) and then handle the request yourself based on the HTTP request type. For example:</p>

<pre><code>class RetrieveMessages(APIView):
    def post(self, request, *args, **kwargs):
        posted_data = self.request.data
        city = posted_data['city']
        return_data = [
            {"echo": city}   
        ]
        return Response(status=200, data=return_data)

     def get....
</code></pre>

python json django rest web