Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
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...
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 ...
WHY REST? 
Better than SOAP 
XML is the stuff of nightmares 
Uses JSON for data structures 
Popular – most modern 3rd part...
COMMON USE-CASES 
Single-Page Applications 
Real-Time Services 
SaaS APIs 
Creating external APIs for existing sites 
Mobi...
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...
ALL TOGETHER NOW:
EXACTLY, WONDER WOMAN.
PROPER SOLUTIONS 
django-rest-framework 
• 900+ forks 
• 304 contributors 
• 3k stars 
• The greatest documentation 
I’ve ...
…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....
1. SERIALIZERS 
“Serializers allow complex data to be 
converted to native Python datatypes that 
can then be easily rende...
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 
...
2. APIVIEWS 
Subclass of Django’s View class 
Simple - has .get() .post(), etc methods 
Some Differences: 
• Requests not ...
2.1 APIVIEWS
3. VIEWSETS 
Similar to Django’s Generic Views. 
“A type of Class-Based View that provides actions like 
.list() and .crea...
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 li...
6. RESPONSES 
Uses content-negotiation to render final content 
(Hence why built-in API Console shows rich interface to us...
IN TRADITIONAL 
DJANGO: 
1. Models/Querysets 
2. Class-Based Views/Mixins 
3. Generic Views 
4. URLs 
5. HTTPRequest 
6. H...
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...
DJANGO-REST-FRAMEWORK 
RESOURCES
RESOURCES 
Docs: http://www.django-rest-framework.org 
Tutorial: http://www.django-rest-framework.org/#tutorial 
IRC: #res...
REST EASY 
WITH 
DJANGO-REST-FRAMEWORK 
MARCEL CHASTAIN (@MARCELCHASTAIN) 
LA DJANGO - 2014-10-28
REST Easy with Django-Rest-Framework
Nächste SlideShare
Wird geladen in …5
×

REST Easy with Django-Rest-Framework

6.932 Aufrufe

Veröffentlicht am

More and more web projects require interfacing with the backend using a REST-ful interface. In this presentation we'll discuss Django-REST-Framework's features and walk through how to integrate it into your next project!

Veröffentlicht in: Software
  • Loggen Sie sich ein, um Kommentare anzuzeigen.

REST Easy with Django-Rest-Framework

  1. 1. REST EASY WITH DJANGO-REST-FRAMEWORK MARCEL CHASTAIN (@MARCELCHASTAIN) LA DJANGO - 2014-10-28
  2. 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
  3. 3. DJANGO-REST-FRAMEWORK ABOUT REST
  4. 4. …BUT I’M NOT TIRED
  5. 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. 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. 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)
  8. 8. SOUNDS SIMPLE ENOUGH!
  9. 9. ALL TOGETHER NOW:
  10. 10. ALL TOGETHER NOW: We should roll our own!
  11. 11. … OK JUST BE SURE TO INCLUDE • serialization/deserialization
  12. 12. … OK JUST BE SURE TO INCLUDE • serialization/deserialization • parsing
  13. 13. … OK JUST BE SURE TO INCLUDE • serialization/deserialization • parsing • model introspection
  14. 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!
  15. 15. ALL TOGETHER NOW:
  16. 16. EXACTLY, WONDER WOMAN.
  17. 17. 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
  18. 18. …YOU’VE GOT OPTIONS
  19. 19. DJANGO-REST-FRAMEWORK THE SETUP
  20. 20. INSTALLATION pip install djangorestframework pip install markdown # optional pip install django-filter # optional
  21. 21. INSTALLATION (2) Add to INSTALLED_APPS # settings.py INSTALLED_APPS = ( ... ‘rest_framework’, )
  22. 22. INSTALLATION (3) Include the login/logout views # urls.py
  23. 23. MODELS
  24. 24. MODELS
  25. 25. MODELS DJANGO MODELS
  26. 26. DJANGO-REST-FRAMEWORK THE CORE COMPONENTS
  27. 27. 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
  28. 28. 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”
  29. 29. 1.1 SERIALIZERS Declarative syntax, similar to Forms/ModelForms Automatically handle single Model instances or Querysets
  30. 30. 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' } }
  31. 31. 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
  32. 32. 2.1 APIVIEWS
  33. 33. 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.
  34. 34. 3.1 VIEWSETS
  35. 35. 3.2 MODELVIEWSETS
  36. 36. 4. URL ROUTERS Automatic URL routing Simple, quick, consistent way of wiring your view logic to a set of URLs
  37. 37. 4. URL ROUTERS Automatic URL routing Simple, quick, consistent way of wiring your view logic to a set of URLs
  38. 38. 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)
  39. 39. 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)
  40. 40. 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:
  41. 41. DJANGO-REST-FRAMEWORK THE DEMO
  42. 42. INSTALLATION (REPO) git clone https://github.com/marcelchastain/drf-demo.git
  43. 43. API RUNNING!
  44. 44. DJANGO-REST-FRAMEWORK CUSTOMIZING
  45. 45. 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)
  46. 46. DJANGO-REST-FRAMEWORK RESOURCES
  47. 47. 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
  48. 48. REST EASY WITH DJANGO-REST-FRAMEWORK MARCEL CHASTAIN (@MARCELCHASTAIN) LA DJANGO - 2014-10-28

×