Piston Module for Django APIs

Posted By : Ishaan Madan | 30-Jun-2017

One of the most frequent requirement is providing third-party support in the we application. In this case we generally make an app which handles the API calls, which sets up the views, andsubsequently compiles the data which is then sent in the supported and safe format such as JSON. This also evoked the need to provide some documentation for these API calls and their usage.

In Django, piston solves all our purpose in a go.

django-piston can be installed using pip,

pip install django-piston

Piston is able to provides every little thing we need to create an API in our site withou. It need a few steps to install and to begin with enlist ‘piston’ into INSTALLED_APPS, so that Django can recognize it.

from django.db import models
from django.contrib.auth.models import User

class Note(models.Model):
    user = models.ForeignKey(User)
    pub_date = models.DateTimeField()
    title = models.CharField(max_length=200)
    body = models.TextField()

    def __unicode__(self):
        return self.title

Further steps involve:

1. create <api> app.

2. Define the namespace in the <api> app in entry level urls.py.

urlpatterns = patterns(”,

#other url mappings listed

(r’^api/’, include(‘piston_test.api.urls’)),
)

# next we need to define the resource.

3. create a file named handler.py projectRoot/api/. The file should containg the following code.

from piston.handler import BaseHandler
from myapp.models import Note

class NoteHandler(BaseHandler):
    allowed_methods = ('GET',)
    model = Note

    def read(self, request, id=None):
        if id:
            return Note.objects.get(pk=id)
        else:
            return Note.objects.all()

4. At Last, we need to map our urls to the resources in handler. Like:

from django.conf.urls.defaults import *
from piston.resource import Resource
from piston_test.api.handlers import NoteHandler

note_handler = Resource(NoteHandler)

urlpatterns = patterns('',
    url(r'^note/(?P<id>[^/]+)/', note_handler),
    url(r'^notes/', note_handler),
)

Interestingly, urlpatterns in url.py appear similar to the norm followed and that is the beauty of using Piston.

—-

Thanks

About Author

Author Image
Ishaan Madan

Ishaan, a skilled technical project manager, excels at breaking down complex projects into manageable tasks. With a background in both technology and project management, he offers a unique perspective to every project he undertakes. His effective communication skills enable him to collaborate seamlessly with both technical and non-technical stakeholders, ensuring everyone is aligned towards shared objectives. He has hands-on experience in utilizing agile methodologies like Scrum and Kanban to drive project management and foster team collaboration. He leverages project management tools such as JIRA, Trello, and Clickup to monitor progress, manage tasks, and facilitate communication among team members and stakeholders. Moreover, his proficiency in full-stack development empowers him to comprehend the technical aspects of projects and provide guidance to developers when necessary. He demonstrates expertise in utilizing popular Python frameworks like Django and Flask, along with data analysis and manipulation libraries such as NumPy and Pandas. On the front-end, Ishaan adeptly employs JavaScript libraries like React and Angular to craft visually appealing and user-friendly interfaces. Additionally, he possesses proficiency in HTML, CSS, and JavaScript for designing responsive and mobile-friendly layouts.

Request for Proposal

Name is required

Comment is required

Sending message..