SlideShare a Scribd company logo
1 of 49
REST EASY 
WITH 
DJANGO-REST-FRAMEWORK 
MARCEL CHASTAIN (@MARCELCHASTAIN) 
LA DJANGO - 2014-10-28
WHAT WE’LL COVER 
• What’s REST? Why/when would we use it? 
• REST challenges 
• Django solutions 
• Installing DRF 
• DRF Core Components (and Django counterparts) 
• Building our Demo API 
• Customizing 
• Resources
DJANGO-REST-FRAMEWORK 
ABOUT 
REST
…BUT I’M NOT TIRED
BUT I’M NOT TIRED 
REST stands for Representational State Transfer 
All 4 CRUD operations 
Uses HTTP requests to: 
• Post data(Create, Update) 
• Read data 
• Delete
WHY REST? 
Better than SOAP 
XML is the stuff of nightmares 
Uses JSON for data structures 
Popular – most modern 3rd party web APIs use RESTful 
endpoints
COMMON USE-CASES 
Single-Page Applications 
Real-Time Services 
SaaS APIs 
Creating external APIs for existing sites 
Mobile Apps 
WebComponents, Polymer, modular site design 
Modern JS Site Frameworks (Angular, Ember, Backbone, etc)
SOUNDS SIMPLE 
ENOUGH!
ALL TOGETHER NOW:
ALL TOGETHER NOW: 
We should 
roll our own!
… OK JUST BE SURE 
TO INCLUDE 
• serialization/deserialization
… OK JUST BE SURE 
TO INCLUDE 
• serialization/deserialization 
• parsing
… OK JUST BE SURE 
TO INCLUDE 
• serialization/deserialization 
• parsing 
• model introspection
… OK JUST BE SURE 
TO INCLUDE 
• serialization/deserialization 
• parsing 
• model introspection 
• relationship traversal 
• pluggable authentication 
• permissions 
• url structure 
• proper HTTP methods 
• pagination 
• forms 
• error handling 
• request filters 
• consistency 
• maybe some generic views 
• request throttling 
• …and tests!
ALL TOGETHER NOW:
EXACTLY, WONDER WOMAN.
PROPER SOLUTIONS 
django-rest-framework 
• 900+ forks 
• 304 contributors 
• 3k stars 
• The greatest documentation 
I’ve seen in a library 
django-tastypie 
• 900+ forks 
• 112 contributors 
• 2.6k stars 
• Delicious-sounding name
…YOU’VE GOT 
OPTIONS
DJANGO-REST-FRAMEWORK 
THE SETUP
INSTALLATION 
pip install djangorestframework 
pip install markdown # optional 
pip install django-filter # optional
INSTALLATION (2) 
Add to INSTALLED_APPS 
# settings.py 
INSTALLED_APPS = ( 
... 
‘rest_framework’, 
)
INSTALLATION (3) 
Include the login/logout views 
# urls.py
MODELS
MODELS
MODELS 
DJANGO MODELS
DJANGO-REST-FRAMEWORK 
THE CORE 
COMPONENTS
IN TRADITIONAL 
DJANGO: 
1. Models/Querysets 
2. Class-Based Views/Mixins 
3. Generic Views 
4. URLs 
5. HTTP Requests 
6. Rendered Responses 
IN DRF: 
1. Serializers 
2. APIViews/Mixins 
3. ViewSets 
4. Routers 
5. HTTP Requests 
6. HTTP Responses
1. SERIALIZERS 
“Serializers allow complex data to be 
converted to native Python datatypes that 
can then be easily rendered in JSON, XML 
or other content types”
1.1 SERIALIZERS 
Declarative syntax, similar to Forms/ModelForms 
Automatically handle single Model instances or Querysets
1.2 SERIALIZERS 
# using it 
>>> note = Note.objects.first() 
>>> serializer = NoteSerializer(note) 
>>> serializer.data 
{u'id': 1, 
'body': u'First, do no harm.', 
'pub_date': datetime.datetime(2014, 10, 28, 11, 23, 30, tzinfo=<UTC>), 
'title': u'Hippocratic Oath', 
'user': { 
'id': 1, 
'username': u'demo', 
'email': u'demo@demo.com' 
} 
}
2. APIVIEWS 
Subclass of Django’s View class 
Simple - has .get() .post(), etc methods 
Some Differences: 
• Requests not normal HTTPRequest (more later) 
• Responses are not normal HTTPResponse (more later) 
• Auth, permissions, throttling done in advance
2.1 APIVIEWS
3. VIEWSETS 
Similar to Django’s Generic Views. 
“A type of Class-Based View that provides actions like 
.list() and .create() instead of .get() and .post()” 
Combine the logic for a set of related views into one class, 
for all the actions you’ll need to take.
3.1 VIEWSETS
3.2 MODELVIEWSETS
4. URL ROUTERS 
Automatic URL routing 
Simple, quick, consistent way of wiring your view logic to a 
set of URLs
4. URL ROUTERS 
Automatic URL routing 
Simple, quick, consistent way of wiring your view logic to a 
set of URLs
5. REQUESTS 
• In an APIView or ViewSet, ‘request’ is a DRF Request. 
• Incoming JSON data in request is processed just like 
Form data 
• Makes request data available as 
• request.DATA (vs .POST) 
• request.FILES 
• request.QUERY_PARAMS (vs .GET)
6. RESPONSES 
Uses content-negotiation to render final content 
(Hence why built-in API Console shows rich interface to us 
but would deliver plain JSON to an AJAX request)
IN TRADITIONAL 
DJANGO: 
1. Models/Querysets 
2. Class-Based Views/Mixins 
3. Generic Views 
4. URLs 
5. HTTPRequest 
6. HTTPResponse 
IN DRF: 
1. Serializers 
2. APIViews/Mixins 
3. ViewSets 
4. URL Routers 
5. DRF ‘Request’ 
6. DRF ‘Response’ 
REVIEW:
DJANGO-REST-FRAMEWORK 
THE DEMO
INSTALLATION (REPO) 
git clone https://github.com/marcelchastain/drf-demo.git
API RUNNING!
DJANGO-REST-FRAMEWORK 
CUSTOMIZING
CUSTOMIZING VIEWS 
• queryset 
• serializer_class 
• filter_class 
• authentication_classes (rest_framework.authentication) 
• permission_classes (rest_framework.permissions) 
• parser_classes (rest_framework.parsers) 
• renderer_classes (rest_framework.renderers) 
• throttle_classes (rest_framework.throttling) 
• paginate_by, max_paginate_by 
(Defaults for most can be set in settings.py)
DJANGO-REST-FRAMEWORK 
RESOURCES
RESOURCES 
Docs: http://www.django-rest-framework.org 
Tutorial: http://www.django-rest-framework.org/#tutorial 
IRC: #restframework on irc.freenode.net 
StackOverflow: ‘django-rest-framework’ tag 
Author: Tom Christie (@_tomchristie) 
Commercial Support: “DAB Apps” http://dabapps.com
REST EASY 
WITH 
DJANGO-REST-FRAMEWORK 
MARCEL CHASTAIN (@MARCELCHASTAIN) 
LA DJANGO - 2014-10-28

More Related Content

What's hot

Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event HandlingWebStackAcademy
 
Python Django tutorial | Getting Started With Django | Web Development With D...
Python Django tutorial | Getting Started With Django | Web Development With D...Python Django tutorial | Getting Started With Django | Web Development With D...
Python Django tutorial | Getting Started With Django | Web Development With D...Edureka!
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial之宇 趙
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to djangoIlian Iliev
 
Angular App Presentation
Angular App PresentationAngular App Presentation
Angular App PresentationElizabeth Long
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction DjangoWade Austin
 
Developing Apps With React Native
Developing Apps With React NativeDeveloping Apps With React Native
Developing Apps With React NativeAlvaro Viebrantz
 
Rest API with Swagger and NodeJS
Rest API with Swagger and NodeJSRest API with Swagger and NodeJS
Rest API with Swagger and NodeJSLuigi Saetta
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariSurekha Gadkari
 
Django - Python MVC Framework
Django - Python MVC FrameworkDjango - Python MVC Framework
Django - Python MVC FrameworkBala Kumar
 
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...Edureka!
 

What's hot (20)

Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Json
JsonJson
Json
 
Json Tutorial
Json TutorialJson Tutorial
Json Tutorial
 
Python Django tutorial | Getting Started With Django | Web Development With D...
Python Django tutorial | Getting Started With Django | Web Development With D...Python Django tutorial | Getting Started With Django | Web Development With D...
Python Django tutorial | Getting Started With Django | Web Development With D...
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to django
 
Laravel Tutorial PPT
Laravel Tutorial PPTLaravel Tutorial PPT
Laravel Tutorial PPT
 
Angular App Presentation
Angular App PresentationAngular App Presentation
Angular App Presentation
 
AngularJS
AngularJS AngularJS
AngularJS
 
React JS part 1
React JS part 1React JS part 1
React JS part 1
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction Django
 
Developing Apps With React Native
Developing Apps With React NativeDeveloping Apps With React Native
Developing Apps With React Native
 
Rest API with Swagger and NodeJS
Rest API with Swagger and NodeJSRest API with Swagger and NodeJS
Rest API with Swagger and NodeJS
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
 
Django - Python MVC Framework
Django - Python MVC FrameworkDjango - Python MVC Framework
Django - Python MVC Framework
 
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
 
Angularjs PPT
Angularjs PPTAngularjs PPT
Angularjs PPT
 
Blazor Full-Stack
Blazor Full-StackBlazor Full-Stack
Blazor Full-Stack
 

Similar to REST Easy with Django-Rest-Framework

Approaches to mobile site development
Approaches to mobile site developmentApproaches to mobile site development
Approaches to mobile site developmentErik Mitchell
 
Plugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGemsPlugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGemsSadayuki Furuhashi
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Search Engines: Best Practice
Search Engines: Best PracticeSearch Engines: Best Practice
Search Engines: Best PracticeYuliya_Prach
 
NoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQL
NoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQLNoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQL
NoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQLAndrew Morgan
 
The future of web development write once, run everywhere with angular js an...
The future of web development   write once, run everywhere with angular js an...The future of web development   write once, run everywhere with angular js an...
The future of web development write once, run everywhere with angular js an...Mark Leusink
 
The future of web development write once, run everywhere with angular.js and ...
The future of web development write once, run everywhere with angular.js and ...The future of web development write once, run everywhere with angular.js and ...
The future of web development write once, run everywhere with angular.js and ...Mark Roden
 
Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTTkevinvw
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBJustin Smestad
 
Onion Architecture with S#arp
Onion Architecture with S#arpOnion Architecture with S#arp
Onion Architecture with S#arpGary Pedretti
 
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...Sencha
 
REST - Why, When and How? at AMIS25
REST - Why, When and How? at AMIS25REST - Why, When and How? at AMIS25
REST - Why, When and How? at AMIS25Jon Petter Hjulstad
 
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptxWRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptxsalemsg
 
WWW09 - Triplify Light-Weight Linked Data Publication from Relational Databases
WWW09 - Triplify Light-Weight Linked Data Publication from Relational DatabasesWWW09 - Triplify Light-Weight Linked Data Publication from Relational Databases
WWW09 - Triplify Light-Weight Linked Data Publication from Relational DatabasesSören Auer
 
REST Enabling Your Oracle Database
REST Enabling Your Oracle DatabaseREST Enabling Your Oracle Database
REST Enabling Your Oracle DatabaseJeff Smith
 
cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123Parag Gajbhiye
 
Continuous Deployment @ AWS Re:Invent
Continuous Deployment @ AWS Re:InventContinuous Deployment @ AWS Re:Invent
Continuous Deployment @ AWS Re:InventJohn Schneider
 
Continuous Integration and Deployment Best Practices on AWS (ARC307) | AWS re...
Continuous Integration and Deployment Best Practices on AWS (ARC307) | AWS re...Continuous Integration and Deployment Best Practices on AWS (ARC307) | AWS re...
Continuous Integration and Deployment Best Practices on AWS (ARC307) | AWS re...Amazon Web Services
 
DBpedia's Triple Pattern Fragments
DBpedia's Triple Pattern FragmentsDBpedia's Triple Pattern Fragments
DBpedia's Triple Pattern FragmentsRuben Verborgh
 

Similar to REST Easy with Django-Rest-Framework (20)

Approaches to mobile site development
Approaches to mobile site developmentApproaches to mobile site development
Approaches to mobile site development
 
Plugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGemsPlugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGems
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Search Engines: Best Practice
Search Engines: Best PracticeSearch Engines: Best Practice
Search Engines: Best Practice
 
NoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQL
NoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQLNoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQL
NoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQL
 
The future of web development write once, run everywhere with angular js an...
The future of web development   write once, run everywhere with angular js an...The future of web development   write once, run everywhere with angular js an...
The future of web development write once, run everywhere with angular js an...
 
The future of web development write once, run everywhere with angular.js and ...
The future of web development write once, run everywhere with angular.js and ...The future of web development write once, run everywhere with angular.js and ...
The future of web development write once, run everywhere with angular.js and ...
 
Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTT
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Onion Architecture with S#arp
Onion Architecture with S#arpOnion Architecture with S#arp
Onion Architecture with S#arp
 
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
 
REST - Why, When and How? at AMIS25
REST - Why, When and How? at AMIS25REST - Why, When and How? at AMIS25
REST - Why, When and How? at AMIS25
 
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptxWRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
 
WWW09 - Triplify Light-Weight Linked Data Publication from Relational Databases
WWW09 - Triplify Light-Weight Linked Data Publication from Relational DatabasesWWW09 - Triplify Light-Weight Linked Data Publication from Relational Databases
WWW09 - Triplify Light-Weight Linked Data Publication from Relational Databases
 
REST Enabling Your Oracle Database
REST Enabling Your Oracle DatabaseREST Enabling Your Oracle Database
REST Enabling Your Oracle Database
 
cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123
 
Django course
Django courseDjango course
Django course
 
Continuous Deployment @ AWS Re:Invent
Continuous Deployment @ AWS Re:InventContinuous Deployment @ AWS Re:Invent
Continuous Deployment @ AWS Re:Invent
 
Continuous Integration and Deployment Best Practices on AWS (ARC307) | AWS re...
Continuous Integration and Deployment Best Practices on AWS (ARC307) | AWS re...Continuous Integration and Deployment Best Practices on AWS (ARC307) | AWS re...
Continuous Integration and Deployment Best Practices on AWS (ARC307) | AWS re...
 
DBpedia's Triple Pattern Fragments
DBpedia's Triple Pattern FragmentsDBpedia's Triple Pattern Fragments
DBpedia's Triple Pattern Fragments
 

Recently uploaded

Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 

Recently uploaded (20)

Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Odoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting ServiceOdoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting Service
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 

REST Easy with Django-Rest-Framework

  • 1. REST EASY WITH DJANGO-REST-FRAMEWORK MARCEL CHASTAIN (@MARCELCHASTAIN) LA DJANGO - 2014-10-28
  • 2. WHAT WE’LL COVER • What’s REST? Why/when would we use it? • REST challenges • Django solutions • Installing DRF • DRF Core Components (and Django counterparts) • Building our Demo API • Customizing • Resources
  • 5. BUT I’M NOT TIRED REST stands for Representational State Transfer All 4 CRUD operations Uses HTTP requests to: • Post data(Create, Update) • Read data • Delete
  • 6. WHY REST? Better than SOAP XML is the stuff of nightmares Uses JSON for data structures Popular – most modern 3rd party web APIs use RESTful endpoints
  • 7. COMMON USE-CASES Single-Page Applications Real-Time Services SaaS APIs Creating external APIs for existing sites Mobile Apps WebComponents, Polymer, modular site design Modern JS Site Frameworks (Angular, Ember, Backbone, etc)
  • 10. ALL TOGETHER NOW: We should roll our own!
  • 11. … OK JUST BE SURE TO INCLUDE • serialization/deserialization
  • 12. … OK JUST BE SURE TO INCLUDE • serialization/deserialization • parsing
  • 13. … OK JUST BE SURE TO INCLUDE • serialization/deserialization • parsing • model introspection
  • 14. … OK JUST BE SURE TO INCLUDE • serialization/deserialization • parsing • model introspection • relationship traversal • pluggable authentication • permissions • url structure • proper HTTP methods • pagination • forms • error handling • request filters • consistency • maybe some generic views • request throttling • …and tests!
  • 16.
  • 18. PROPER SOLUTIONS django-rest-framework • 900+ forks • 304 contributors • 3k stars • The greatest documentation I’ve seen in a library django-tastypie • 900+ forks • 112 contributors • 2.6k stars • Delicious-sounding name
  • 21. INSTALLATION pip install djangorestframework pip install markdown # optional pip install django-filter # optional
  • 22. INSTALLATION (2) Add to INSTALLED_APPS # settings.py INSTALLED_APPS = ( ... ‘rest_framework’, )
  • 23. INSTALLATION (3) Include the login/logout views # urls.py
  • 28. IN TRADITIONAL DJANGO: 1. Models/Querysets 2. Class-Based Views/Mixins 3. Generic Views 4. URLs 5. HTTP Requests 6. Rendered Responses IN DRF: 1. Serializers 2. APIViews/Mixins 3. ViewSets 4. Routers 5. HTTP Requests 6. HTTP Responses
  • 29. 1. SERIALIZERS “Serializers allow complex data to be converted to native Python datatypes that can then be easily rendered in JSON, XML or other content types”
  • 30. 1.1 SERIALIZERS Declarative syntax, similar to Forms/ModelForms Automatically handle single Model instances or Querysets
  • 31. 1.2 SERIALIZERS # using it >>> note = Note.objects.first() >>> serializer = NoteSerializer(note) >>> serializer.data {u'id': 1, 'body': u'First, do no harm.', 'pub_date': datetime.datetime(2014, 10, 28, 11, 23, 30, tzinfo=<UTC>), 'title': u'Hippocratic Oath', 'user': { 'id': 1, 'username': u'demo', 'email': u'demo@demo.com' } }
  • 32. 2. APIVIEWS Subclass of Django’s View class Simple - has .get() .post(), etc methods Some Differences: • Requests not normal HTTPRequest (more later) • Responses are not normal HTTPResponse (more later) • Auth, permissions, throttling done in advance
  • 34. 3. VIEWSETS Similar to Django’s Generic Views. “A type of Class-Based View that provides actions like .list() and .create() instead of .get() and .post()” Combine the logic for a set of related views into one class, for all the actions you’ll need to take.
  • 37. 4. URL ROUTERS Automatic URL routing Simple, quick, consistent way of wiring your view logic to a set of URLs
  • 38. 4. URL ROUTERS Automatic URL routing Simple, quick, consistent way of wiring your view logic to a set of URLs
  • 39. 5. REQUESTS • In an APIView or ViewSet, ‘request’ is a DRF Request. • Incoming JSON data in request is processed just like Form data • Makes request data available as • request.DATA (vs .POST) • request.FILES • request.QUERY_PARAMS (vs .GET)
  • 40. 6. RESPONSES Uses content-negotiation to render final content (Hence why built-in API Console shows rich interface to us but would deliver plain JSON to an AJAX request)
  • 41. IN TRADITIONAL DJANGO: 1. Models/Querysets 2. Class-Based Views/Mixins 3. Generic Views 4. URLs 5. HTTPRequest 6. HTTPResponse IN DRF: 1. Serializers 2. APIViews/Mixins 3. ViewSets 4. URL Routers 5. DRF ‘Request’ 6. DRF ‘Response’ REVIEW:
  • 43. INSTALLATION (REPO) git clone https://github.com/marcelchastain/drf-demo.git
  • 46. CUSTOMIZING VIEWS • queryset • serializer_class • filter_class • authentication_classes (rest_framework.authentication) • permission_classes (rest_framework.permissions) • parser_classes (rest_framework.parsers) • renderer_classes (rest_framework.renderers) • throttle_classes (rest_framework.throttling) • paginate_by, max_paginate_by (Defaults for most can be set in settings.py)
  • 48. RESOURCES Docs: http://www.django-rest-framework.org Tutorial: http://www.django-rest-framework.org/#tutorial IRC: #restframework on irc.freenode.net StackOverflow: ‘django-rest-framework’ tag Author: Tom Christie (@_tomchristie) Commercial Support: “DAB Apps” http://dabapps.com
  • 49. REST EASY WITH DJANGO-REST-FRAMEWORK MARCEL CHASTAIN (@MARCELCHASTAIN) LA DJANGO - 2014-10-28

Editor's Notes

  1. NoteSerializer.user would have been a plain primary key, but we use a nested serializer