Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django Rest Framework

Hassan
November 19, 2014

Django Rest Framework

Introduction into the Django Rest Framework

Hassan

November 19, 2014
Tweet

More Decks by Hassan

Other Decks in Technology

Transcript

  1. What is Django REST Framework • Add on package/app for

    Django • Provides various utilities to expose a RESTful API through your Django application • Highly configurable • HTML Browsable API
  2. Django REST Basics Serializers • “Translates” data • Similar to

    Django Forms - serializer.is_valid() • Serializer vs ModelSerializer Views/Viewsets • Does “stuff” • Same pattern as Django's Views • Function based vs Class based
  3. Django REST Basics Authentication • BasicAuthentication, TokenAuthentication, SessionAuthentication, OAuthAuthentication, etc.

    Permissions • AllowAny, IsAuthenticated, DjangoModelPermissions, etc. Typically uses the authentication info in request.user and request.auth
  4. Django REST Basics Routing • Defines the mapping between your

    API and URLs • DRF supports automatic URL routing • SimpleRouter, DefaultRouter, Custom
  5. Some More DRF Things Save/Delete hooks Scope = View pre_save(self,

    obj) post_save(self, obj, created=False) pre_delete(self, obj) post_delete(self, obj)
  6. Some More DRF Things Customizing the validation process Scope =

    Serializers 1. Field level validation .validate_<fieldname>(self, attrs, source) 2. Object level validation .validate(self, attrs)
  7. Some More DRF Things Version your APIs MyProject api __init__.py

    v3.py v2_5.py http://test.api.com/customers http://test.api.com/v3.0/customers http://test.api.com/v2.5/customers
  8. Some More DRF Things Unit testing – test like a

    Django app APIRequestFactory Django's RequestFactory class APIClient Django's Client class APITestCase Identical to Django's unit test method from rest_framework.test import APITestCase