blog/source/_posts/Tastypie.md
Ching 9690121403 feat(init project): add all existing files
add all existing files

Signed-off-by: Ching <loooching@gmail.com>
2022-02-02 19:04:18 +08:00

38 lines
1.2 KiB
Markdown

---
title: Tastypie
date: 2016-05-04 12:02:00
---
#### 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
![flow](https://impythonist.files.wordpress.com/2016/04/tastypie_ill.png?w=800)
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.