Loading video player…

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 DRF ViewSet 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.13, Django 5.1.2, and the Django REST Framework 3.15.2.

Download

Sample Code (.zip)

54.6 KB
Download

Course Slides (.pdf)

815.7 KB

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 in this course has been updated and it has been tested using Python 3.13, Django 5.1.2, and the Django REST Framework 3.15.2. 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.

01:28 One final note, as part of our process at Real Python, all the code here has been formatted with Black. The reason I bring this up is because the default code generated by Django’s admin command is not Black compliant.

01:42 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 jango. For each of the major concepts inside of Django, like views, models, and URLs, the DRF provides REST variance on these.

03:15 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.

03:29 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.

03:50 The most popular being JSON. Other mechanisms are also supported as well as there being third-party libraries. In addition to regular data like JSON, DRF also comes with a handy web tool that can be useful when you’re debugging.

04:05 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.