# Tastypie

2016年5月4日

#### Resources in Tastypie

Resources are the heart of Tastypie. By defining a resource we can actually convert a model into an API stream. The data is automatically converted into API response.

Understanding the process of creating a resource.

1. Import ModelResource from Tastypie.
2. Import models from services app
3. Create custom resource by inheriting ModelResource and link app model in inner Meta class of resource.

Add API URL in the urls.py of app.

#### Dehydrating the JSON data

Dehydration in Tastypie means making alterations before sending data to the client. Suppose we need to send capitalized product names instead of small letters. Now we see two kinds of dehydrate methods.

##### Dehydrate_field method

This `dehydrate_field` is uesd to modify field on the response JSON.

##### Dehydrate method

Dehydrate method is useful for aadding additional fields to bundle (response data).

Similarly using `hydrate` method we can alter the bundle data which is generated from request at the time of PUT or POST methods.