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