SlideShare a Scribd company logo
13
Most read
16
Most read
17
Most read
Marcos PereiraBuilding a web API with Django
Review
•Retrieves a Resource
•CacheableGET
•Creates a new ResourcePOST
•Updates an existing ResourcePUT
•Removes a ResourceDELETE
2
Review
u Client-Server
u Stateless
u Json, XML, etc.
u HATEOAS (Hipemedia As The Engine Of Application
State)
3
Alternatives ?
u SOAP
u WSDLs
u WebSockets
4
What’s DRF?
Django REST framework is a powerful and
flexible toolkit for building Web APIs.
5
What’s DRF?
u Some reasons you might want to use REST framework:
u The Web browsable API is a huge usability win for your developers.
u Authentication policies including packages
for OAuth1a and OAuth2.
u Serialization that supports both ORM and non-ORM data sources.
u Customizable all the way down - just use regular function-based
views if you don't need the more powerful features.
u Extensive documentation, and great community support.
u Used and trusted by large companies such
as Mozilla and Eventbrite.
6
What’s DRF?
traditional
1. Models/Querysets
2. Class-Based Views/Mixins
3. Generic Views
4. URLs
5. HTTP Requests
6. Rendered Responses
Django Rest Framework
1. Serializers
2. APIViews/Mixins
3. ViewSets
4. Routers
5. HTTP Requests
6. HTTP Responses
7
Quickstart
u Requirements:
u Python (2.7, 3.2, 3.3, 3.4, 3.5)
u Django (1.7+, 1.8, 1.9)
u Installation:
8
Quickstart
u Installation:
9
Quickstart
u Settings:
10
CoffeeManger
u Frontend
https://github.com/marcospereirampj/frontend-coffee-
manager
u Backend
https://github.com/marcospereirampj/backend-coffee-
manager
11
DRF - Serialization
u Creating a Serializer class
u Working with Serializers
u serializers.Serializer
u Using ModelSerializers
Serializers ~ Django’s Forms
12
DRF - Requests and Responses
u Request objects:
u REST framework introduces a Request object that extends
the regular HttpRequest, and provides more flexible request
parsing. The core functionality of the Request object is
the request.data attribute, which is similar to request.POST,
but more useful for working with Web APIs.
u Response objects:
u REST framework also introduces a Response object, which is
a type of TemplateResponse that takes unrendered content
and uses content negotiation to determine the correct
content type to return to the client.
u Status codes:
u Using numeric HTTP status codes in your views doesn't always
make for obvious reading, and it's easy to not notice if you
get an error code wrong. REST framework provides more
explicit identifiers for each status code, such
as HTTP_400_BAD_REQUEST in the status module. It's a good
idea to use these throughout rather than using numeric
identifiers.
13
DRF - Requests and Responses
u Wrapping API views
u REST framework provides two wrappers you can use to
write API views:
u The @api_view decorator for working with function based
views.
u The APIView class for working with class based views.
14
DRF - Class Based Views
u We can also write our API views using class based
views, rather than function based views. As we'll see
this is a powerful pattern that allows us to reuse
common functionality, and helps us keep our
code DRY (don't repeat yourself).
u Writing our API using class based views
u Using mixins
u Using generic class based views
15
DRF - Relationships & Hyperlinked APIs
u At the moment relationships within our API are
represented by using primary keys. In this part of the
tutorial we'll improve the cohesion and discoverability of
our API, by instead using hyperlinking for relationships.
u Using primary keys.
u Using hyperlinking between entities.
u Using a unique identifying slug field on the related entity.
u Using the default string representation of the related entity.
u Nesting the related entity inside the parent representation.
u Some other custom representation.
16
DRF - ViewSets & Routers
u REST framework includes an abstraction for dealing
with ViewSets, that allows the developer to
concentrate on modeling the state and interactions
of the API, and leave the URL construction to be
handled automatically, based on common
conventions.
u ViewSet classes are almost the same thing
as View classes, except that they provide operations
such as read, or update, and not method handlers
such as get or put.
u Using Routers
u Binding ViewSets
17
DRF - Authentication & Permissions
u Authenticating with the API
u Adding required permissions to views
u Custom Permissions
18
Questions & Discussion
Marcos Pereira
marcos.pereira@itec.al.gov.br
marcospereira.mpj@gmail.com
19

More Related Content

What's hot (20)

PPTX
React.js - The Dawn of Virtual DOM
Jimit Shah
 
PDF
Rest in flask
Hamid Feizabadi
 
PDF
Api presentation
Tiago Cardoso
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PPTX
Web development with django - Basics Presentation
Shrinath Shenoy
 
PDF
Django Introduction & Tutorial
之宇 趙
 
PPTX
Introduction to Django
Knoldus Inc.
 
PPTX
Rest API
Rohana K Amarakoon
 
PPTX
Django Girls Tutorial
Kishimi Ibrahim Ishaq
 
PDF
A Basic Django Introduction
Ganga Ram
 
PDF
Rest api with Python
Santosh Ghimire
 
PDF
Introduction to django
Ilian Iliev
 
PDF
Angular
Lilia Sfaxi
 
PPT
SQLITE Android
Sourabh Sahu
 
PPTX
django
Mohamed Essam
 
PPTX
RESTful API - Best Practices
Tricode (part of Dept)
 
PPTX
Intro to React
Justin Reock
 
PDF
Intro to Web Development Using Python and Django
Chariza Pladin
 
PPT
Learn REST API with Python
Larry Cai
 
PPT
Introduction to the Web API
Brad Genereaux
 
React.js - The Dawn of Virtual DOM
Jimit Shah
 
Rest in flask
Hamid Feizabadi
 
Api presentation
Tiago Cardoso
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Web development with django - Basics Presentation
Shrinath Shenoy
 
Django Introduction & Tutorial
之宇 趙
 
Introduction to Django
Knoldus Inc.
 
Django Girls Tutorial
Kishimi Ibrahim Ishaq
 
A Basic Django Introduction
Ganga Ram
 
Rest api with Python
Santosh Ghimire
 
Introduction to django
Ilian Iliev
 
Angular
Lilia Sfaxi
 
SQLITE Android
Sourabh Sahu
 
RESTful API - Best Practices
Tricode (part of Dept)
 
Intro to React
Justin Reock
 
Intro to Web Development Using Python and Django
Chariza Pladin
 
Learn REST API with Python
Larry Cai
 
Introduction to the Web API
Brad Genereaux
 

Viewers also liked (7)

PDF
Django admin
Petter Arvidsson
 
PDF
Django rest framework tips and tricks
xordoquy
 
PDF
Boost your django admin with Grappelli
Andy Dai
 
PDF
Django REST Framework
Load Impact
 
PDF
Customizing the Django Admin
Lincoln Loop
 
PDF
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Nina Zakharenko
 
PDF
Create responsive websites with Django, REST and AngularJS
Hannes Hapke
 
Django admin
Petter Arvidsson
 
Django rest framework tips and tricks
xordoquy
 
Boost your django admin with Grappelli
Andy Dai
 
Django REST Framework
Load Impact
 
Customizing the Django Admin
Lincoln Loop
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Nina Zakharenko
 
Create responsive websites with Django, REST and AngularJS
Hannes Hapke
 
Ad

Similar to Django Rest Framework - Building a Web API (20)

PDF
Implement nested serializers in the Django rest framework
Ritika
 
PDF
Building an API with Django and Django REST Framework
Christopher Foresman
 
PPTX
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Caktus Group
 
PPTX
Drf
Ibrahim Kasim
 
PDF
Rest apis with DRF
Dharmit Shah
 
PDF
Django Rest Framework - tips & trick
Luca Zacchetti
 
PPTX
Django REST Framework 2022fffffffff.pptx
ThirzaAhmadTsaqif
 
PPTX
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Caktus Group
 
PDF
Django Rest Framework + React
wsvincent
 
PDF
DRF React
wsvincent
 
PPTX
Django with REST API Online Training - NareshIT
avinashnit
 
PDF
Introduction to Django REST Framework
AmitHadole
 
PDF
ERRest: the Basics
WO Community
 
PDF
Scalable web application architecture
postrational
 
PDF
Building RESTful APIs
Silota Inc.
 
PDF
Python Ireland Nov 2010 - RESTing with Django
Python Ireland
 
PDF
FastAPI - Rest Architecture - in english.pdf
inigraha
 
PPTX
React django
Heber Silva
 
PPTX
Api crash
Hoang Nguyen
 
PPTX
Api crash
Luis Goldster
 
Implement nested serializers in the Django rest framework
Ritika
 
Building an API with Django and Django REST Framework
Christopher Foresman
 
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Caktus Group
 
Rest apis with DRF
Dharmit Shah
 
Django Rest Framework - tips & trick
Luca Zacchetti
 
Django REST Framework 2022fffffffff.pptx
ThirzaAhmadTsaqif
 
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Caktus Group
 
Django Rest Framework + React
wsvincent
 
DRF React
wsvincent
 
Django with REST API Online Training - NareshIT
avinashnit
 
Introduction to Django REST Framework
AmitHadole
 
ERRest: the Basics
WO Community
 
Scalable web application architecture
postrational
 
Building RESTful APIs
Silota Inc.
 
Python Ireland Nov 2010 - RESTing with Django
Python Ireland
 
FastAPI - Rest Architecture - in english.pdf
inigraha
 
React django
Heber Silva
 
Api crash
Hoang Nguyen
 
Api crash
Luis Goldster
 
Ad

Recently uploaded (20)

PDF
Best Software Development at Best Prices
softechies7
 
PDF
AI Software Development Process, Strategies and Challenges
Net-Craft.com
 
PDF
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
PDF
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
PDF
Rewards and Recognition (2).pdf
ethan Talor
 
PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 
PDF
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
PPTX
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
PPTX
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
PDF
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
Best Software Development at Best Prices
softechies7
 
AI Software Development Process, Strategies and Challenges
Net-Craft.com
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
Automated Test Case Repair Using Language Models
Lionel Briand
 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
Rewards and Recognition (2).pdf
ethan Talor
 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 

Django Rest Framework - Building a Web API

  • 1. Marcos PereiraBuilding a web API with Django
  • 2. Review •Retrieves a Resource •CacheableGET •Creates a new ResourcePOST •Updates an existing ResourcePUT •Removes a ResourceDELETE 2
  • 3. Review u Client-Server u Stateless u Json, XML, etc. u HATEOAS (Hipemedia As The Engine Of Application State) 3
  • 4. Alternatives ? u SOAP u WSDLs u WebSockets 4
  • 5. What’s DRF? Django REST framework is a powerful and flexible toolkit for building Web APIs. 5
  • 6. What’s DRF? u Some reasons you might want to use REST framework: u The Web browsable API is a huge usability win for your developers. u Authentication policies including packages for OAuth1a and OAuth2. u Serialization that supports both ORM and non-ORM data sources. u Customizable all the way down - just use regular function-based views if you don't need the more powerful features. u Extensive documentation, and great community support. u Used and trusted by large companies such as Mozilla and Eventbrite. 6
  • 7. What’s DRF? traditional 1. Models/Querysets 2. Class-Based Views/Mixins 3. Generic Views 4. URLs 5. HTTP Requests 6. Rendered Responses Django Rest Framework 1. Serializers 2. APIViews/Mixins 3. ViewSets 4. Routers 5. HTTP Requests 6. HTTP Responses 7
  • 8. Quickstart u Requirements: u Python (2.7, 3.2, 3.3, 3.4, 3.5) u Django (1.7+, 1.8, 1.9) u Installation: 8
  • 12. DRF - Serialization u Creating a Serializer class u Working with Serializers u serializers.Serializer u Using ModelSerializers Serializers ~ Django’s Forms 12
  • 13. DRF - Requests and Responses u Request objects: u REST framework introduces a Request object that extends the regular HttpRequest, and provides more flexible request parsing. The core functionality of the Request object is the request.data attribute, which is similar to request.POST, but more useful for working with Web APIs. u Response objects: u REST framework also introduces a Response object, which is a type of TemplateResponse that takes unrendered content and uses content negotiation to determine the correct content type to return to the client. u Status codes: u Using numeric HTTP status codes in your views doesn't always make for obvious reading, and it's easy to not notice if you get an error code wrong. REST framework provides more explicit identifiers for each status code, such as HTTP_400_BAD_REQUEST in the status module. It's a good idea to use these throughout rather than using numeric identifiers. 13
  • 14. DRF - Requests and Responses u Wrapping API views u REST framework provides two wrappers you can use to write API views: u The @api_view decorator for working with function based views. u The APIView class for working with class based views. 14
  • 15. DRF - Class Based Views u We can also write our API views using class based views, rather than function based views. As we'll see this is a powerful pattern that allows us to reuse common functionality, and helps us keep our code DRY (don't repeat yourself). u Writing our API using class based views u Using mixins u Using generic class based views 15
  • 16. DRF - Relationships & Hyperlinked APIs u At the moment relationships within our API are represented by using primary keys. In this part of the tutorial we'll improve the cohesion and discoverability of our API, by instead using hyperlinking for relationships. u Using primary keys. u Using hyperlinking between entities. u Using a unique identifying slug field on the related entity. u Using the default string representation of the related entity. u Nesting the related entity inside the parent representation. u Some other custom representation. 16
  • 17. DRF - ViewSets & Routers u REST framework includes an abstraction for dealing with ViewSets, that allows the developer to concentrate on modeling the state and interactions of the API, and leave the URL construction to be handled automatically, based on common conventions. u ViewSet classes are almost the same thing as View classes, except that they provide operations such as read, or update, and not method handlers such as get or put. u Using Routers u Binding ViewSets 17
  • 18. DRF - Authentication & Permissions u Authenticating with the API u Adding required permissions to views u Custom Permissions 18