What You Should Know About Django REST Framework
02-20, 11:05–11:30 (US/Pacific), Prerecorded Talks

When I started writing Django REST Framework code in 2017, there was a lot I didn't know, like how class-based views worked and what a serializer was.

After 3+ years of using DRF, I want to share the things I've learned that make writing DRF code easier and faster for me. You will learn how to save time and lines of code by using DRF's built-in viewsets (and what a viewset is), when to skip the viewset and use a built-in generic APIView, and how to add custom endpoints (actions) to your viewsets. You'll also learn how modular DRF can be when you customize built-in methods or use different serializers for different parts of your viewset, and how tools like Classy DRF can help.

You will walk away from this talk ready to start your first DRF project, make your existing projects simpler and cleaner, or (at the very least) with a better understanding of how a class-based view works.

You will get the most out of this talk if you have beginner-level experience in Python and Django. A tenuous, vague understanding of how class-based views in Django work would be helpful, but is not necessary. Having completed a Django REST Framework tutorial would also be helpful.

My goal with this talk is to make Django REST Framework easier and more approachable for developers who are new to DRF. Many people feel that you should start in DRF with the APIView and a manual serializer, but I disagree! DRF ships with some powerful tools that make developers' lives easier, and many people never even learn what those tools are. Using the built-in tools like ModelViewSet, coupled with always having Classy DRF open in a tab, helped me gain a full understanding of how DRF worked (and a better understanding of how Django worked).

This talk is not a DRF tutorial.

  • 1 min: Intro, why the audience can trust me
  • 3 min (running total: 4 min): ModelViewSet and how it's composed of all the mixins that allow you to perform different operations on your database objects (CreateModelMixin, RetrieveModelMixin, etc.).
  • 2 min (running total: 6 min) The basic attributes that are added to most Viewsets like permission_classes, queryset, and serializer_class.
  • 5 min (running total: 11 min) How to override built-in methods to customize behavior, like overriding get_serializer_class() to return different serializers for List vs. Detail requests.
  • 4 min (running total: 15 min) How to add multiple permission classes to permission_classes to create modular permissions. Note: This will not cover how to write custom permission classes, only how to use them.
  • 5 min (running total: 20 min) Overview of the generic views, noting how they are also just combinations of the APIView and the necessary mixins. Notes on when you might use them (example: you want a viewset that allows all actions except delete)
  • 4 min (running total: 24 min) How to customize the API by adding an action to a viewset when you need something different than the built-in CRUD operations
  • 1 min (running total: 25 min) Outro, where to find me to ask questions, where to find the slides, links to further learning

I will be referring to Classy DRF throughout the talk. In my experience, many people find the Classy Class-Based Views, Classy DRF, and that family of helper websites confusing, until they learn how to use them. I want to help the audience learn to use these tools.

Note: This is a new talk for me. I tend to rehearse my talks a lot, so if time is too tight I will know that ahead of time. The section I would cut for time would be "Overview of the generic views," because this idea will be introduced in the ModelViewSet section.

Lacey Williams Henschel is a Django consultant with REVSYS with about 10 years of experience programming in Python and Django. She's a skilled technical writer, coding teacher, workshop mentor, and conference speaker who is passionate about helping complex topics go from "intimidating" to "totally doable" for people. Previously, she's spoken at DjangoCon US, DjangoCon Europe, PyGotham, Wagtail Space US, and PostgresOpen, among other conferences. She lives in Oregon with her husband, daughter, and two cats.