[Answered ]-When should I be using an API framework for Django (or other)?

2đź‘Ť

âś…

This is the kind of question where ideally it should be self explanatory if you indeed know what you want to design in the first place. I don’t mean that in a condensing way, but rather just saying if your design goal is clear, then you should know when an API framework is valuable. You are misinterpreting the statement from tastypie docs: “You need an API that is RESTful and uses HTTP well.”. If you see, it is part of a section called “Why Tastypie”. What it is saying is that you need tastypie if what you seek is a RESTful API that uses HTTP well. They aren’t telling you that you do in fact need this. Only that it is a recommended solution to someone who does have these needs.

From a general perspective, lets assume you want to create an API for your django project. There are different ways to design your API, but a very well accepted format is REST. This is a pattern that makes URLs very understandable and predictable in terms of reading, updating, creating, and deleting. Now lets assume that you want to create your API using this popular and well accepted REST pattern…

When creating an API, there is a certain amount of functionality that is pretty common, and almost boilerplate. You will have to query models in your django project and return a full or subset of fields, or even a custom format of that data. This is where we start getting into the benefits of an API framework…

Let me pose this question to you… Would you want to code everything from scratch if a framework existed that had a robust set of features covering the most common needs of an API? It’s almost the same situation as why you chose to use django instead of coding your own web site.

There are two very popular API frameworks for django: tastypie & piston
They both offer similar features that get you up and running quite fast. You can create a handler class that maps right to a model and by default gives you full CRUD access to it by a number of formats (json, yaml, etc). Both of the frameworks assume you want to usually bind a handler around a django model, though you aren’t restricted to it.

Authentication: The framework handles it for you almost as easy as creating the instance of the type you want to use and assigned it to the handler.

Formats: (mentioned earlier) Your apis can automatically handle multiple data formats like json, yaml, xml, etc.

Bottom line is that the frameworks are there to remove the tedious task of constantly repeating this functionality. All the hard work is done. You just need to code your specific handling functions and map them to urls. Ready to go.

Obviously you could just write some views and manually handle your request object and your return value, if all you want is a few simple handlers. But when you want to create more robust API’s, save yourself the time and use something that lays the ground work for you.

Beyond this, just look at the tutorials for tastypie or piston to see how easy it is to start exporting resources that are already bound to your models.

👤jdi

Leave a comment