SlideShare a Scribd company logo
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:
REST Easy with Django-Rest-Framework
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 (20)

PPTX
Introduction to Django Rest Framework
bangaloredjangousergroup
 
PDF
Django REST Framework
Load Impact
 
PDF
Django Introduction & Tutorial
之宇 趙
 
PDF
React new features and intro to Hooks
Soluto
 
PPTX
SASS - CSS with Superpower
Kanchha kaji Prajapati
 
PPTX
Json
Steve Fort
 
PPT
Django, What is it, Why is it cool?
Tom Brander
 
PPTX
Angular tutorial
Rohit Gupta
 
PPTX
Django - Python MVC Framework
Bala Kumar
 
PPTX
jQuery
Jay Poojara
 
PDF
Django Tutorial | Django Web Development With Python | Django Training and Ce...
Edureka!
 
PDF
Introduction to Django REST Framework, an easy way to build REST framework in...
Zhe Li
 
PPTX
Creating custom Validators on Reactive Forms using Angular 6
AIMDek Technologies
 
KEY
JSON-LD: JSON for Linked Data
Gregg Kellogg
 
PPTX
React Workshop
GDSC UofT Mississauga
 
KEY
Introduction Django
Wade Austin
 
PPTX
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
PDF
React Router: React Meetup XXL
Rob Gietema
 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
Introduction to Django Rest Framework
bangaloredjangousergroup
 
Django REST Framework
Load Impact
 
Django Introduction & Tutorial
之宇 趙
 
React new features and intro to Hooks
Soluto
 
SASS - CSS with Superpower
Kanchha kaji Prajapati
 
Django, What is it, Why is it cool?
Tom Brander
 
Angular tutorial
Rohit Gupta
 
Django - Python MVC Framework
Bala Kumar
 
jQuery
Jay Poojara
 
Django Tutorial | Django Web Development With Python | Django Training and Ce...
Edureka!
 
Introduction to Django REST Framework, an easy way to build REST framework in...
Zhe Li
 
Creating custom Validators on Reactive Forms using Angular 6
AIMDek Technologies
 
JSON-LD: JSON for Linked Data
Gregg Kellogg
 
React Workshop
GDSC UofT Mississauga
 
Introduction Django
Wade Austin
 
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
React Router: React Meetup XXL
Rob Gietema
 
ReactJS presentation.pptx
DivyanshGupta922023
 

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

KEY
Approaches to mobile site development
Erik Mitchell
 
PDF
Plugin-based software design with Ruby and RubyGems
Sadayuki Furuhashi
 
PDF
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
PPTX
Search Engines: Best Practice
Yuliya_Prach
 
PDF
NoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQL
Andrew Morgan
 
PPTX
The future of web development write once, run everywhere with angular.js and ...
Mark Roden
 
PDF
The future of web development write once, run everywhere with angular js an...
Mark Leusink
 
PDF
Python & Django TTT
kevinvw
 
PDF
Introduction to MongoDB
Justin Smestad
 
PPTX
Onion Architecture with S#arp
Gary Pedretti
 
PPT
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
Sencha
 
PDF
REST - Why, When and How? at AMIS25
Jon Petter Hjulstad
 
PPTX
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
salemsg
 
PPTX
WWW09 - Triplify Light-Weight Linked Data Publication from Relational Databases
Sören Auer
 
PPTX
REST Enabling Your Oracle Database
Jeff Smith
 
PPTX
Django course
Nagi Annapureddy
 
PDF
Continuous Deployment @ AWS Re:Invent
John Schneider
 
PDF
DBpedia's Triple Pattern Fragments
Ruben Verborgh
 
PDF
Firebase slide
Apaichon Punopas
 
Approaches to mobile site development
Erik Mitchell
 
Plugin-based software design with Ruby and RubyGems
Sadayuki Furuhashi
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
Search Engines: Best Practice
Yuliya_Prach
 
NoSQL and SQL - Why Choose? Enjoy the best of both worlds with MySQL
Andrew Morgan
 
The future of web development write once, run everywhere with angular.js and ...
Mark Roden
 
The future of web development write once, run everywhere with angular js an...
Mark Leusink
 
Python & Django TTT
kevinvw
 
Introduction to MongoDB
Justin Smestad
 
Onion Architecture with S#arp
Gary Pedretti
 
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
Sencha
 
REST - Why, When and How? at AMIS25
Jon Petter Hjulstad
 
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
salemsg
 
WWW09 - Triplify Light-Weight Linked Data Publication from Relational Databases
Sören Auer
 
REST Enabling Your Oracle Database
Jeff Smith
 
Django course
Nagi Annapureddy
 
Continuous Deployment @ AWS Re:Invent
John Schneider
 
DBpedia's Triple Pattern Fragments
Ruben Verborgh
 
Firebase slide
Apaichon Punopas
 
Ad

Recently uploaded (20)

PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Ad

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!
  • 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'[email protected]' } }
  • 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

  • #31: NoteSerializer.user would have been a plain primary key, but we use a nested serializer