Large Problems in Django, Mostly Solved: APIs¶

This is the third part of my Large Problems Series. The first two were Search and Database Migrations.

A lot of efforts have come and gone in the Django space, trying to provide a API’s that do various things. Some have tried to give you automatic CRUD based off your models or by abstracting the admin, others have extended Django serializers to provide some kind of functionality, and there have been lots of other approaches.

I think that Piston hits a sweet spot for creating APIs. It has a lot of nice little features, and handles the general use case well. It is also abstract enough that it allows you to provide your own layer on top of it with ease.

Piston¶ Piston has three major philosophical concepts that are important; Resources, Handlers, and Emitters. A Resource is the “thing” that you are trying to represent in your API, the domain object. This could be a blog post, a user, or anything else. A Handler is how you do something with that resource. It is a lot like a view, where you get the request and it delegates to different functions based on what you want to do (create, update, read) with it. The Handler will return some kind of object, and the Emitter’s job is to output this. It is where you choose the format (xml, json, yaml) and other information about how the data is returned. The way these things are abstracted makes it really easy to create a REST API. In fact, the documentation has a full working example. I would like to talk about some of the nicer features and abilities of Piston. This is not a tutorial, but more a pointer, so that you know it exists and kicks ass. The Piston Documentation is decent in regard to getting you going.