django rest framework - getting started

To get started with django rest framework, you gotta started reading from the back cuz for some reasons these guys like to give out the best implementation in the end.

Anyways, go ahead to create your virtualenv

pip install django
pip install djangorestframework

# Set up a new project with a single application startproject tutorial .  # Note the trailing '.' character
cd tutorial startapp quickstart
cd ..
Let's start of with Request/Response object. As these remain critical for our development efforts and work with

What it is saying on 2nd like, is we are creating a root api for our application and the "name" properties you see in 3rd and 4th line are references for us to do reverse look up which you will see later. 

urlpatterns = [
url(r'^$', views.api_root),
url(r'^deploy/$', views.SimpleDeployer.as_view(), name='deploy-list'),
url(r'^deploy/simple/$', views.SimpleDeployer.as_view(), name='simple-list'),
url(r'^deploy/accesskey/$', views.AccessKey.as_view())

This gives us this view and ability to link to its other child urls.

Next, we can work with our views.  Edit and then add in the following codes. I am inheriting from APIView which automatically gives REST based functionality - GET  POST PUT verbs as indicated here. You need to follow the standard method name too, otherwise it won't appear in your API when you browse it. 

Notice that i have method called get, post and put. 

class SimpleDeployer(APIView):
def get(self, request, format=None):
return Response('Simple Deployer')
def post(self, request, format=None):
return Response('Simple Deployer')
def put(self, request, format=None):
return Response('Simple Deployer')

For CRUB specific implementation, you can use "generics.ListCreateAPIView" or "generics.RetrieveUpdateDestroyAPIView"

Looks like we're ready, Fire up your django runserver and have a look at what you've achieved. 


Popular posts from this blog

PySpark - Working with JDBC Sqlite database