REST API creation with django-tastypie
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.
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.
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.
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: