SlideShare a Scribd company logo
1 of 16
Build and Customize RESTful Web
Services with Django-Tastypie
@gauravtoshniwal
Gaurav Toshniwal
Co-founder, WireddIn Interactive Pvt. Ltd.
(Mobile and Web App development)
Outline of the Talk
 Why REST?
 Why Tastypie?
 Why not Tastypie?
 Basic API setup from a model
 DEMO
 Related Models/ Exclude/ Include/ Serializers
 Ordering/Filtering/Bulk Operations
 Authentication
 Authorization
 Customization (Complex Filters/Hydrate/Dehydrate)
 O & A
About REST
 Client-Server
 Stateless
 Cacheable
 Layered System
 Uniform Interface
About REST: Vocabulary
 GET
 POST
 PUT
 PATCH
 DELETE
About REST: RESTful Web APIs
 A RESTful web API (also called a RESTful web service) is a
web API implemented using HTTP and REST principles. It is
a collection of resources, with four defined aspects:
 the base URI for the web API, such as
http://example.com/resources/
 the Internet media type of the data supported by the web
API. This is often JSON but can be any other valid Internet
media type provided that it is a valid hypertext standard.
 the set of operations supported by the web API using HTTP
methods (e.g., GET, PUT, POST, or DELETE).
 The API must be hypertext driven.
What Is Tastypie?
 "Tastypie is an webservice API framework for Django. It
provides a convenient, yet powerful and highly
customizable, abstraction for creating REST-style
interfaces.”
 Ranked high in Django-Packages ranking algorithm
Why not Tastypie?
Why Tastypie?
 Makes Sense – quick and feature-rich
 Well Tested
 Good features and support, fits well into existing ORM
thinking
 Totally Extensible
 Serialization methods
 Authentication and authorization in-built
 Other features: Throttling
Basic API setup: Define model
from django.db import models
class Todo(models.Model):
todo = models.CharField(max_length=100, blank=False)
done = models.BooleanField("Done",blank=True,
default=False)
def __unicode__(self):
return "%s" % self.todo
Basic API setup: Define Resources
from tastypie.resources import ModelResource
from tastypie import fields
from tastypie.serializers import Serializer
from tastypie.constants import ALL, ALL_WITH_RELATIONS
from todo.models import Todo
class TodoResource(ModelResource):
class Meta:
queryset = Todo.objects.all()
resource_name = 'todo’
Basic API setup: Hook up URLs
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
from tastypie.api import Api
api = Api(api_name='v1')
from todo.api.resources import TodoResource
api.register(TodoResource())
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^api/', include(api.urls)),
)
Basic API setup: DEMO
 Schema
 POST demo (create data)
 View data in Admin
 GET demo (retrieve data)
 without format=json
 with format=json
 with format=xml, yaml
 /set/1;5/?format=json
 DELETE demo
Related Models/ Exclude/ Include/
Serializers
class UserTodo(models.Model):
todo = models.CharField(max_length=100, blank=False)
done = models.BooleanField("Done",blank=True,
default=False)
due = models.DateField(blank=True)
created = models.DateField(auto_now_add=True)
user = models.ForeignKey(User)
def __unicode__(self):
return "%s" % self.todo
Related Models/ Exclude/ Include/
Serializers
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
excludes = ['email',
'password',
'is_superuser']
serializer = Serializer(formats=['json'])
class UserTodoResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = UserTodo.objects.all()
resource_name = 'user_todo'
fields = ['todo', 'user','due']
serializer = Serializer(formats=['json'])
authorization = Authorization()
Exclude fields from API output
Include only these fields
Built-in Authorize everyone for
everything option
Foreign key
To be added:
 Request-response cycle
 Filtering
 Advanced Data preparation (customized logic):
 The Dehydrate Cycle (customize output)
 The Hydrate Cycle (customize input)
 Authentication
 Authorization
Request Response Cycle
Request
• Resource.wrap_view(‘dispatch_list’)
Dispatch_list
dispatch
• Checks
• Allowed methods
• If the class has a method that can handle the request (get_list)
• Is_authenticated
• Is_authorized
• Throttle_check
Get_list
• Obj_get_list
• Build_filters
• Get_objects_list (gets the queryset)
• apply_authorization_limits (to limit the set the user can work with)
• sorting
• pagination
• Full_dehydrate raw data to objects)
Create_response
• Serialization
• Return httpResponse

More Related Content

What's hot

深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVCJace Ju
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Engine Yard
 
You're Doing It Wrong
You're Doing It WrongYou're Doing It Wrong
You're Doing It Wrongbostonrb
 
Simplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 StepsSimplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 Stepstutec
 
Desenvolvendo APIs usando Rails - Guru SC 2012
Desenvolvendo APIs usando Rails - Guru SC 2012Desenvolvendo APIs usando Rails - Guru SC 2012
Desenvolvendo APIs usando Rails - Guru SC 2012Rafael Felix da Silva
 
ACL in CodeIgniter
ACL in CodeIgniterACL in CodeIgniter
ACL in CodeIgnitermirahman
 
Cakefest 2010: API Development
Cakefest 2010: API DevelopmentCakefest 2010: API Development
Cakefest 2010: API DevelopmentAndrew Curioso
 
Rails 3 Beautiful Code
Rails 3 Beautiful CodeRails 3 Beautiful Code
Rails 3 Beautiful CodeGreggPollack
 
Teaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumTeaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumJeroen van Dijk
 
Codeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept ImplementationCodeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept ImplementationAbdul Malik Ikhsan
 
Zend - Installation And Sample Project Creation
Zend - Installation And Sample Project Creation Zend - Installation And Sample Project Creation
Zend - Installation And Sample Project Creation Compare Infobase Limited
 
devise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwandevise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwanTse-Ching Ho
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Dave Hulbert
 

What's hot (20)

深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVC
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel
 
Zend framework
Zend frameworkZend framework
Zend framework
 
You're Doing It Wrong
You're Doing It WrongYou're Doing It Wrong
You're Doing It Wrong
 
Simplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 StepsSimplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 Steps
 
Complex Sites with Silex
Complex Sites with SilexComplex Sites with Silex
Complex Sites with Silex
 
Desenvolvendo APIs usando Rails - Guru SC 2012
Desenvolvendo APIs usando Rails - Guru SC 2012Desenvolvendo APIs usando Rails - Guru SC 2012
Desenvolvendo APIs usando Rails - Guru SC 2012
 
ACL in CodeIgniter
ACL in CodeIgniterACL in CodeIgniter
ACL in CodeIgniter
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Cakefest 2010: API Development
Cakefest 2010: API DevelopmentCakefest 2010: API Development
Cakefest 2010: API Development
 
ADF 2.4.0 And Beyond
ADF 2.4.0 And BeyondADF 2.4.0 And Beyond
ADF 2.4.0 And Beyond
 
Zend Framework
Zend FrameworkZend Framework
Zend Framework
 
Rails 3 Beautiful Code
Rails 3 Beautiful CodeRails 3 Beautiful Code
Rails 3 Beautiful Code
 
Apache Click
Apache ClickApache Click
Apache Click
 
Teaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumTeaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in Titanium
 
Codeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept ImplementationCodeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept Implementation
 
Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
Zend - Installation And Sample Project Creation
Zend - Installation And Sample Project Creation Zend - Installation And Sample Project Creation
Zend - Installation And Sample Project Creation
 
devise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwandevise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwan
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)
 

Similar to Django Tastypie 101

Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesNina Zakharenko
 
Designing CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsDesigning CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsNeil Crookes
 
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)Amazon Web Services
 
Testwarez 2013 - Warsztat SoapUI
Testwarez 2013 - Warsztat SoapUI Testwarez 2013 - Warsztat SoapUI
Testwarez 2013 - Warsztat SoapUI Jacek Okrojek
 
Django for Beginners
Django for BeginnersDjango for Beginners
Django for BeginnersJason Davies
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Djangocolinkingswood
 
Webservices in SalesForce (part 1)
Webservices in SalesForce (part 1)Webservices in SalesForce (part 1)
Webservices in SalesForce (part 1)Mindfire Solutions
 
Federico Feroldi - Scala microservices
Federico Feroldi - Scala microservicesFederico Feroldi - Scala microservices
Federico Feroldi - Scala microservicesScala Italy
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Mario Cardinal
 
ASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web applicationASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web applicationAMARAAHMED7
 
Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAERon Reiter
 
A Minimalist’s Attempt at Building a Distributed Application
A Minimalist’s Attempt at Building a Distributed ApplicationA Minimalist’s Attempt at Building a Distributed Application
A Minimalist’s Attempt at Building a Distributed ApplicationDavid Hoerster
 
Getting Started with Zend Framework
Getting Started with Zend FrameworkGetting Started with Zend Framework
Getting Started with Zend FrameworkJuan Antonio
 
Swift LA Meetup at eHarmony- What's New in Swift 2.0
Swift LA Meetup at eHarmony- What's New in Swift 2.0Swift LA Meetup at eHarmony- What's New in Swift 2.0
Swift LA Meetup at eHarmony- What's New in Swift 2.0Claire Townend Gee
 
The Role of Python in SPAs (Single-Page Applications)
The Role of Python in SPAs (Single-Page Applications)The Role of Python in SPAs (Single-Page Applications)
The Role of Python in SPAs (Single-Page Applications)David Gibbons
 

Similar to Django Tastypie 101 (20)

Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
 
Designing CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsDesigning CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIs
 
Web api
Web apiWeb api
Web api
 
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
 
Testwarez 2013 - Warsztat SoapUI
Testwarez 2013 - Warsztat SoapUI Testwarez 2013 - Warsztat SoapUI
Testwarez 2013 - Warsztat SoapUI
 
Crafting [Better] API Clients
Crafting [Better] API ClientsCrafting [Better] API Clients
Crafting [Better] API Clients
 
Django for Beginners
Django for BeginnersDjango for Beginners
Django for Beginners
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Webservices in SalesForce (part 1)
Webservices in SalesForce (part 1)Webservices in SalesForce (part 1)
Webservices in SalesForce (part 1)
 
Federico Feroldi - Scala microservices
Federico Feroldi - Scala microservicesFederico Feroldi - Scala microservices
Federico Feroldi - Scala microservices
 
Web API with ASP.NET MVC by Software development company in india
Web API with ASP.NET  MVC  by Software development company in indiaWeb API with ASP.NET  MVC  by Software development company in india
Web API with ASP.NET MVC by Software development company in india
 
Standards of rest api
Standards of rest apiStandards of rest api
Standards of rest api
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.
 
ASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web applicationASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web application
 
Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAE
 
A Minimalist’s Attempt at Building a Distributed Application
A Minimalist’s Attempt at Building a Distributed ApplicationA Minimalist’s Attempt at Building a Distributed Application
A Minimalist’s Attempt at Building a Distributed Application
 
Getting Started with Zend Framework
Getting Started with Zend FrameworkGetting Started with Zend Framework
Getting Started with Zend Framework
 
Swift LA Meetup at eHarmony- What's New in Swift 2.0
Swift LA Meetup at eHarmony- What's New in Swift 2.0Swift LA Meetup at eHarmony- What's New in Swift 2.0
Swift LA Meetup at eHarmony- What's New in Swift 2.0
 
Api testing
Api testingApi testing
Api testing
 
The Role of Python in SPAs (Single-Page Applications)
The Role of Python in SPAs (Single-Page Applications)The Role of Python in SPAs (Single-Page Applications)
The Role of Python in SPAs (Single-Page Applications)
 

Recently uploaded

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 

Recently uploaded (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 

Django Tastypie 101

  • 1. Build and Customize RESTful Web Services with Django-Tastypie @gauravtoshniwal Gaurav Toshniwal Co-founder, WireddIn Interactive Pvt. Ltd. (Mobile and Web App development)
  • 2. Outline of the Talk  Why REST?  Why Tastypie?  Why not Tastypie?  Basic API setup from a model  DEMO  Related Models/ Exclude/ Include/ Serializers  Ordering/Filtering/Bulk Operations  Authentication  Authorization  Customization (Complex Filters/Hydrate/Dehydrate)  O & A
  • 3. About REST  Client-Server  Stateless  Cacheable  Layered System  Uniform Interface
  • 4. About REST: Vocabulary  GET  POST  PUT  PATCH  DELETE
  • 5. About REST: RESTful Web APIs  A RESTful web API (also called a RESTful web service) is a web API implemented using HTTP and REST principles. It is a collection of resources, with four defined aspects:  the base URI for the web API, such as http://example.com/resources/  the Internet media type of the data supported by the web API. This is often JSON but can be any other valid Internet media type provided that it is a valid hypertext standard.  the set of operations supported by the web API using HTTP methods (e.g., GET, PUT, POST, or DELETE).  The API must be hypertext driven.
  • 6. What Is Tastypie?  "Tastypie is an webservice API framework for Django. It provides a convenient, yet powerful and highly customizable, abstraction for creating REST-style interfaces.”  Ranked high in Django-Packages ranking algorithm
  • 8. Why Tastypie?  Makes Sense – quick and feature-rich  Well Tested  Good features and support, fits well into existing ORM thinking  Totally Extensible  Serialization methods  Authentication and authorization in-built  Other features: Throttling
  • 9. Basic API setup: Define model from django.db import models class Todo(models.Model): todo = models.CharField(max_length=100, blank=False) done = models.BooleanField("Done",blank=True, default=False) def __unicode__(self): return "%s" % self.todo
  • 10. Basic API setup: Define Resources from tastypie.resources import ModelResource from tastypie import fields from tastypie.serializers import Serializer from tastypie.constants import ALL, ALL_WITH_RELATIONS from todo.models import Todo class TodoResource(ModelResource): class Meta: queryset = Todo.objects.all() resource_name = 'todo’
  • 11. Basic API setup: Hook up URLs from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() from tastypie.api import Api api = Api(api_name='v1') from todo.api.resources import TodoResource api.register(TodoResource()) urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), (r'^api/', include(api.urls)), )
  • 12. Basic API setup: DEMO  Schema  POST demo (create data)  View data in Admin  GET demo (retrieve data)  without format=json  with format=json  with format=xml, yaml  /set/1;5/?format=json  DELETE demo
  • 13. Related Models/ Exclude/ Include/ Serializers class UserTodo(models.Model): todo = models.CharField(max_length=100, blank=False) done = models.BooleanField("Done",blank=True, default=False) due = models.DateField(blank=True) created = models.DateField(auto_now_add=True) user = models.ForeignKey(User) def __unicode__(self): return "%s" % self.todo
  • 14. Related Models/ Exclude/ Include/ Serializers class UserResource(ModelResource): class Meta: queryset = User.objects.all() resource_name = 'user' excludes = ['email', 'password', 'is_superuser'] serializer = Serializer(formats=['json']) class UserTodoResource(ModelResource): user = fields.ForeignKey(UserResource, 'user') class Meta: queryset = UserTodo.objects.all() resource_name = 'user_todo' fields = ['todo', 'user','due'] serializer = Serializer(formats=['json']) authorization = Authorization() Exclude fields from API output Include only these fields Built-in Authorize everyone for everything option Foreign key
  • 15. To be added:  Request-response cycle  Filtering  Advanced Data preparation (customized logic):  The Dehydrate Cycle (customize output)  The Hydrate Cycle (customize input)  Authentication  Authorization
  • 16. Request Response Cycle Request • Resource.wrap_view(‘dispatch_list’) Dispatch_list dispatch • Checks • Allowed methods • If the class has a method that can handle the request (get_list) • Is_authenticated • Is_authorized • Throttle_check Get_list • Obj_get_list • Build_filters • Get_objects_list (gets the queryset) • apply_authorization_limits (to limit the set the user can work with) • sorting • pagination • Full_dehydrate raw data to objects) Create_response • Serialization • Return httpResponse

Editor's Notes

  1. Graphic showing the stack
  2. - Define django model - models.py - register the app in settings.py - Create an admin interface, add dummy data - enable in urls.py - enable in settings.py - create admin.py - add dummy data
  3. - DEMO - demo without format=json - demo with format=json - demo with format=xml, yaml - /schema/?format=json - /set/1;5/?format=json - POST demo - DELETE demo