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!
1 of 49
Downloaded 257 times
More Related Content
REST Easy with Django-Rest-Framework
1. REST EASY
WITH
DJANGO-REST-FRAMEWORK
MARCEL CHASTAIN (@MARCELCHASTAIN)
LA DJANGO - 2014-10-28
2. WHAT WELL COVER
Whats 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 IM 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)
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!
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
32. 2. APIVIEWS
Subclass of Djangos 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 Djangos 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 youll 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)
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)