Demystifying the Django REST Framework
Submitted by Haris Ibrahim K. V. (@harisibrahimkv) on Sunday, 4 December 2016
The tutorial covers the basics of Django REST Framework, building of
RESTful apis from DRF’s perspective and a hands on approach all along
while we learn together.
This will be an intermediate tutorial on Django Rest Framework. For a
first time user, DRF presents a ton of magic. Things work, but you end
up not knowing or understanding what is happening behind the scenes.
If you would want to tweak something, you wouldn’t know where to start
or what to look at.
I’ve had quite a few frustrating moments trying to figure out what
method mapped to which action, and which ones to override when. The
fact that DRF does not impose the REST architecture confused me to a
good extent as well. Nested relationships was another beast which was
hard to tame.
This tutorial will build an app - not a full fledged front end design
but the endpoints that are required, and will showcase implementing
endpoints using complete abstraction that DRF provides including
Routers, ViewSets and builtin Serializers as the first step.
The second step will break the ViewSets and implement them as normal
class based views, showing what happens behind the scenes. How the
methods are mapped, how it flows through from the moment a request
hits one of the views.
The third step will involve writing the routers by hand, using normal
views and how to fine tune serializers to add your custom fields, etc.
The rough plan is:
0 - 20 minutes: Brushing up REST architecture and general ice breaker. 20 - 50 minutes: Setting up a Django project and DRF installation. 50 - 1h 20: Implementing the first endpoint, with all abstraction and goodness of DRF. 1h 20 - 1h 40: Breaking down the ViewSet into simple APIViews and explaining the methods. 1h 40 - 2h 00: Breaking it further down to manually writing the routers, normal views, etc. 2h 00 - 2h 30: Implementation of a nested relationship. 2h 30 - 3h 00: General discussion, pitfalls and do's as well as dont's.
- virtualenv (https://virtualenv.pypa.io/en/stable/installation/)
Unless you know well about managing your system wide installation and
versions (and even if you do), please have virtualenv installed and
- Python 3.5 / Python 2.7
- Django==1.10.3 (pip install django)
If you want to use Python 2.7, that’s totally fine. Just make sure
Django works properly for your setup and have djangorestframework
We will use sqlite as the database, which usually comes bundled
along with Python. But just double check to make sure that you have it
installed. (Hint: Simply starting a Django project and running “python
manage.py migrate” should work flawlessly and leave you with a
db.sqlite3 file in your root folder).
Former Geek herder @HasGeek. Used to be Python tamer @Eventifier and
now @aksharadotorg. An amateur writer. A ‘psycho’ path author.
Husband. Trainer. Teacher. Coach.