Tastypie documentation - Deepstash
Tastypie documentation

Tastypie documentation

Tastypie documentation

django-tastypie.readthedocs.io

5

Keep reading for FREE

Introduction

This article is recommended to be read having some prerequisites. You should be familiar with HTTP Methods and some basics regarding Django.

If that's not your case, please check the resources below before proceeding with the article:

1

33 reads

What is Tastypie

Tastypie is a  framework for Django that helps programmers to develop RESTful APIs on top of their already existing projects.

1

19 reads

What is a Tastypie Resource

As a general rule, each group of endpoints is seen as a resource. When I say a group of endpoints I mean a collection of HTTP methods on a specific route (e.g. GET, POST and DELETE on /api/entity).

These resources have a lot of features, which include allowing you to access the endpoint with specific HTTP methods, permitting access only if you are authenticated, paginating the results etc.

1

14 reads

Types of endpoints

In Tastypie, there are 3 types of endpoints:

  1. list
  2. detail
  3. custom

List endpoints return a list of objects (as the name implies) and it looks like `host_address/api/entity`. 

Detail endpoints return a single instance of an object and it looks similar to the list endpoint, except for the fact that it expects a path param, the ID of the object, to identify the instance that will be returned (e.g. `/api/entity/[id]`).

Custom endpoints are routes made by developers to solve any use-cases different from the default ones. They don't have a specific format, offering devs more freedom.

1

12 reads

Endpoint function structure

Each endpoint needs a function to manipulate and return the data. But these functions have a standard format

  • They have as parameters `self` (being a class method inside the resource), `request` (which is the `Request` instance of the client's API call
  • They expect the returned value to be a HttpResponse. For best-practice and convenience, there is a method called `create_response` that takes care of that. All you need to pass is the request and the object that you want to be included

1

13 reads

Routing a request

Now let's focus on what's the steps for a request to be fulfilled on a Tastypie endpoint.

When a request is received from a client, Django checks for any existing routes inside `urlpatterns`, the variable with all the available routes inside the project.

If a pattern was found, Django will call the associated view (function or class) with the following parameters:

  1. an instance of HttpRequest
  2. extra params got from the URL

If the URL didn't match any pattern, then Django will take care of it via an appropriate error handling view.

1

6 reads

Handling a request inside Tastypie

Once Django redirects the request to a Tastypie resource, it checks for the available URLs and lets Tastypie do the rest.  

Assuming the URL is`host-domain/api/v1/entity/` (which is a list view):

If the view was matched, `Resource.wrap_view('dispatch_list')` is called. The wrap_view method inside the Resource takes care of basic error handling and serialisation for the errors. The 'dispatch_list' parameter means that Resource.dispatch_list will be called next (a thin wrapper around the dispatch method).

1

8 reads

Resource.dispatch method

The dispatch method makes sure the followings are fulfilled:

  • the HTTP Method inside the request is allowed in the resource
  • the resource has a method that can handle the request (e.g. get_list, get_detail, get_custom_endpoint)
  • the user is authenticated
  • the user did not exceed their throttle

1

11 reads

Response generators methods naming

The response is generated using the specific class method of the resource. If it's a default endpoint, the methods look like [HTTP_method]_[view_type] (e.g. get_list, post_detail) and if it's a custom endpoint, the method's name is the defined route for the endpoint (e.g. add_more_entries)

1

12 reads

Generating the response for `get_list`

The following actions will be done when calling the get_list:

  1. All the available objects are fetched via `Resource.obj_get_list`
  2. Objects are sorted via `Resource.apply_sorting` and paginated with the built-in Paginator
  3. the data will be dehydrated, meaning Tastypie will keep only the allowed fields
  4. `Resource.create_response` is called, which serializes the data in the proper format and returns a HttpResponse object with the data

Observation: For POST/PUT, there will be an extra step, the hydrate cycle, that will take the request payload and convert it in raw data, for DB storage

1

11 reads

Once the Tastypie returned the HttpResponse object, Django will send it to the client which made the request.

As a conclusion, you can consider Tastypie endpoints some Django views which return processed data in standardized formats (or HTTP errors, based on the case).

1

10 reads

CURATED BY

teni

Normal person with peculiar dreams @unkl's protégé @cristianmezei's disciple

My takes after reading the Tastypie documentation, regarding how a request is handled inside an endpoint

MORE LIKE THIS

Foreign Key on Models | Django documentation

Foreign Key on Models | Django documentation

Foreign Key on Models | Django documentation

docs.djangoproject.com

Save vs Update in Django

Save vs Update in Django

Save vs Update in Django

helmut.dev

6 IDEAS

10 Interview Questions Every JavaScript Developer Should Know

10 Interview Questions Every JavaScript Developer Should Know

Ready for the next level?

Read Like a Pro

stash-superman-illustration

Explore the World’s

Best Ideas

200,000+ ideas on pretty much any topic. Created by the smartest people around & well-organized so you can explore at will.

An Idea for Everything

Explore the biggest library of insights. And we've infused it with powerful filtering tools so you can easily find what you need.

Knowledge Library

Powerful Saving & Organizational Tools

Save ideas for later reading, for personalized stashes, or for remembering it later.

# Personal Growth

Take Your Ideas

Anywhere

Organize your ideas & listen on the go. And with Pro, there are no limits.

Listen on the go

Just press play and we take care of the words.

Never worry about spotty connections

No Internet access? No problem. Within the mobile app, all your ideas are available, even when offline.

Get Organized with Stashes

Ideas for your next work project? Quotes that inspire you? Put them in the right place so you never lose them.

Join

2 Million Stashers

4.8

Stars

5,740 Reviews

App Store

4.7

Stars

72,690 Reviews

Google Play

Shankul Varada

Best app ever! You heard it right. This app has helped me get back on my quest to get things done while equipping myself with knowledge everyday.

Ashley Anthony

This app is LOADED with RELEVANT, HELPFUL, AND EDUCATIONAL material. It is creatively intellectual, yet minimal enough to not overstimulate and create a learning block. I am exceptionally impressed with this app!

Sean Green

Great interesting short snippets of informative articles. Highly recommended to anyone who loves information and lacks patience.

samz905

Don’t look further if you love learning new things. A refreshing concept that provides quick ideas for busy thought leaders.

Ghazala Begum

Even five minutes a day will improve your thinking. I've come across new ideas and learnt to improve existing ways to become more motivated, confident and happier.

Giovanna Scalzone

Brilliant. It feels fresh and encouraging. So many interesting pieces of information that are just enough to absorb and apply. So happy I found this.

Laetitia Berton

I have only been using it for a few days now, but I have found answers to questions I had never consciously formulated, or to problems I face everyday at work or at home. I wish I had found this earlier, highly recommended!

Jamyson Haug

Great for quick bits of information and interesting ideas around whatever topics you are interested in. Visually, it looks great as well.

Read & Learn

20x Faster

without
deepstash

with
deepstash

with

deepstash

Access to 200,000+ ideas

Access to the mobile app

Unlimited idea saving & library

Unlimited history

Unlimited listening to ideas

Downloading & offline access

Personalized recommendations

FAQ

Claim Your Limited Offer

Get Deepstash Pro

BLACK FRIDAY

75% OFF | 1-Year Pro Subscription

Claim Offer