Flow Through The Request/Response Cycle
Tastypie can be thought of as a set of class-based view that provide the API functionality. All routing/middleware/response-handling aspectss are the same as a typical Django app. Where the differs is in the view itself.
Walking through what a GET request to a list endpoint looks like:
The
Resource.urlsare checked by Django’s url resolvers.On a match for the list view,
Resource.wrap_view('dispatch_list')is called.wrap_viewprovides basic error handling & allows for returning serilized errors.Because dispatch_list was passed to
wrap_view,Resource.dispatch_listis called next. This is a thin wrapper aroundResource.dispatch.dispatchdoes a bunch of havy lifting. It ensures:- the requested HTTP method is in
allowed_methos(method_check). - the class has a method that can handle the request(
get_list) - the user is authenticated(
is_authenticated) - the user has no exceeded their throttle(
throttle_check).
At this point,
dispatchactually calls the requested method (get_list).- the requested HTTP method is in
get_listdoes the actual work of API. It does:- A fetch of the available objects via
Resource.obj_get_list. In the case ofModelResource, this builds the ORM filters to apply (ModelResource.build_filters). It then gets theQuerySetviaModelResource.get_object_list(which performsResource.authorized_read_listto possibly limit the set the user can work with) and applies the built filters to it. - It then sorts the objects based on user input (
ModelResource.apply_sorting). - Then it paginates the results using the supplied
Paginator& pulls out the data to be serialized. - The objects in the page have
full_dehydrateapplied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports. - Finally, it calls
Resource.create_response.
- A fetch of the available objects via
create_responseis a shortcut method that:- Determines the desired response format (
Resource.determine_format). - Serializes the data given to it in the proper format.
- Returns a Django
HttpResponse(200 OK) with the serialized data.
- Determines the desired response format (
We bubble back up the call stack to
dispatch. The last thingdispatchdoes is potentially store that a request occured for future throttling (Resource.log_throttled_access) then either returns theHttpResponseor wraps whatever data came back in a response (so Django doesn’t freak out).

