Building With Django REST Framework (Overview)
REST is a loosely defined protocol for listing, creating, changing, and deleting data on your server over HTTP. The Django REST framework (DRF) is a toolkit built on top of the Django web framework that reduces the amount of code you need to write to create REST interfaces.
In this course you’ll learn about:
- The REST protocol
- DRF
Serializers
and how to use them with Django objects - Using Django
views
and DRFViewSet
classes to create REST end-points - Multiple flavors of renderers and how to control their output
- Specifying permissions and limiting who can see what data in your REST API
This course assumes you are familiar with Django models, views, URL patterns, and the admin interface. For more background on these topics see:
Here are resources for curl and Black:
Sample code provided in the Supporting Materials drop-down was tested using Python 3.9.0, Django 3.1.2, and Django REST framework 3.12.1.
00:00 Welcome to Django REST Framework. My name is Chris and I will be your guide. In this course, you will learn what is REST and how to use it, how to get started with the Django REST Framework, or DRF as it’s known to its friends, and details on the various parts of the DRF, such as serializers, views, ViewSets, routers, permissions, and actions.
00:28 A little bit of housekeeping before I get started. First off, this course assumes you are familiar with Django already. You will need to understand Django’s models, views, urls, and a little bit of administration files. If you’re new to Django, you might want to check out other courses first.
00:45
Some links to try are in the description below. Additionally, I’ll be using the command line tool curl
to visit certain URLs during demonstrations. If you’re following along on the command line, you’ll need curl
installed. Depending on what operating system you’re running, you may or may not have it already. If it isn’t installed by default, you can get it at the URL here.
01:09
The code samples shown inside of this course were tested with Python 3.9.0, Django 3.1.2, and Django REST Framework 3.12.1. There isn’t anything that I’m doing here that is particularly version-dependent, but if you run into some little weird corner case, that might be why. One final note, as part of our process at Real Python, all the code here has been formatted with Black
.
01:34
The reason I bring this up is because the default code generated by Django’s admin command is not Black
compliant, so if you’re comparing what you’re seeing on my screen with your own settings file, you may notice some presentation differences, and that’s because Black
has changed it.
01:53 The Django REST Framework is of course all about REST. REST is a protocol used to send data back and forth between servers. It’s the standard that isn’t a standard.
02:04 You can think of it as a protocol guideline—a set of ideas that are commonly used across a lot of interfaces, but not as codified as something like SOAP.
02:13 REST is built on top of existing HTTP methods and URLs, and this is how you can be a standard without being a standard. The underlying concepts in it are standards.
02:24 The HTTP methods are used to do things like list available objects, get details on an object, create an object, update an object, patch an object—the difference between updating and patching is whether or not you have to provide all of the fields for an object—and finally, deleting an object.
02:47 The format of the data sent back and forth in the body of the REST command is up to the implementer. The most common mechanisms for serializing data are JSON and XML.
02:58 The Django REST framework is a Django library that takes the concepts in REST and ties them to the models and views inside of Django. For each of the major concepts inside of Django like views, models, and urls, the DRF provides REST variants on these. First off, Django models get paired with DRF serializers. The serializers specify how to turn the content of a Django model into JSON or XML to be sent to a client. DRF views and ViewSets are specialized Django views used to control what REST protocol mechanisms are exposed through your API. Like Django URLs, DRF routers map the views to the actual URLs exposed. Out of the box, DRF supports a variety of data rendering interfaces, the most popular being JSON. Other mechanisms are also supported, as well as there being third-party libraries.
03:58 In addition to regular data like JSON, DRF also comes with a handy web tool that can be useful when you’re debugging. All of this is tied together with a handy permission structure to make sure that you’re only exposing the data that you mean to.
04:14 In the next lesson, I’ll give you an overview of REST in order to help you get started.
Become a Member to join the conversation.