REST API creation with django-tastypie
django-tastypie is probably the best at the moment tool for REST API creation in Django applications. The code is on github and documentation is on readthedocs.org.
With tastypie it's easy to create REST API that will allow easy web access to data stored in models - get, update, edit or delete. In this article I'll show some basic usage of tastypie.
Quick startThe installation is typical:
Now we can start creating resource classes that will be translated into the REST API by tastypie. Documentation presents a "api" folder: APPLICATION_NAME/api/resources.py to store those classes. The location where you can write them isn't fixed. I used api.py file in the application folder.
Creating the APIFor this example I used one of my applications that has a JobOffer model holding job offers. Basic resource class for it (or any other model) would look like so:
Each resource class inherits from ModelResource. In the Meta subclass it needs to have queryset defined. The resource_name is optional - can be used to override the resource name used in URLs.
We need the hook the resource class in the urls.py file. Tastypie has it's own URL generator. We register the resource class:And add tastypie routing to urls: API is done. Under http://localhost:8000/api/v1/joboffer/?format=json we will get a list of entries. Under http://localhost:8000/api/v1/joboffer/schema/?format=json tastypie will show schema of the entry, as well some info about the API (allowe methods for example). Using POST/PUT/DELETE requests you could edit/add/delete entries. The URL structure for a resource is described in the a href="http://django-tastypie.readthedocs.org/en/latest/tutorial.html#hooking-up-the-resource-s">documentation.
Some extra featuresWe can tune the resource class to fit out needs. For example we can use allowed_methods to limit allowed methods (like only GET to give read-only access). fields can be used to specify which model fields should be available through the API. Here is an example: There are also methods that allow altering data before it's serialized by tastypie. For example dehydrate allows you to add non-model fields into the serialized datasets:
bundle.data is a dictionary with data of a given entry that will be serialized. bundle.obj is the entry object - on which I call one of model methods.Tastypie also supports authentication. Basic login required access would look like so: