Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Building With Django REST Framework (Summary)

The Django REST Framework is a powerful set of tools giving you the ability to create REST interfaces on top of the Django web framework with a minimum of additional code. Now you know how to list, create, change, and delete data on your server over HTTP using

In this course you learned 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

Here are additional resources about Django, REST, and DRF:

Download

Sample Code (.zip)

54.6 KB
Download

Course Slides (.pdf)

815.7 KB

00:00 In the previous lesson, I showed you a sample single-page application. In this final lesson, I’ll summarize what you’ve learned. REST is a loosely-defined protocol that allows you to list, create, update, patch, and delete data on your server.

00:16 It uses existing HTTP methods and URLs to perform the actions. The Django REST Framework, or DRF, works in conjunction with Django to perform REST activities on top of Django model objects and views.

00:32 Inside DRF, the serializers are responsible for turning your data into payloads and taking payloads and turning them into your data. The ModelSerializer maps Django ORM objects.

00:47 This means it takes very little code to convert something you’ve already got into a payload. Objects with relationships on the server side can be serialized together and nested into parent objects.

01:01 Although the most common use of serialization is to serialize Django ORM objects, DRF serializers are able to serialize generic Python classes. You can pretty much serialize or deserialize almost anything that you want.

01:17 You implement a REST endpoint with a view on the Django side. The DRF provides decorators to wrap your views and REST Response objects that know how to work with the serializers. In addition to the views, the DRF provides a series of classes called ViewSets.

01:34 These classes group together multiple HTTP methods or REST actions and manage them together. This is particularly useful when you’re serializing a Django ORM object.

01:45 You can create a ViewSet that has all of the REST action points for that ORM object in just a few lines of code. You can create a DRF Router object to register one or more ViewSets, and it will automatically map that ViewSet to the necessary URL patterns. You can manage who sees what data and what they’re allowed to do with it by specifying permissions and querysets associated with a ViewSet.

02:12 You can also add custom actions to a ViewSet in order to augment the base REST calls.

02:19 The DRF Response object takes information out of the serializer and turns it into a payload. It does this through a renderer. DRF supports several different ways of rendering your data. By default, it gives both a JSON and a web-based interface for your payloads.

02:39 You can also control which renderer’s being used and how things are rendered through changing settings or decorators of your views.

02:49 For further reading and minutia on the DRF, go to the Django REST Framework website. The website also maintains a huge list of third-party packages that may already implement that corner case that you’ve been worried about.

03:03 Take a look at this URL for more information.

03:07 I hope this course has been valuable to you. Thanks for your attention.

Avatar image for allapopovarus

allapopovarus on Nov. 3, 2021

Here is a huge gap in intermediate and advanced levels. There is no bsmodals in static folder in sample code…

Avatar image for Christopher Trudeau

Christopher Trudeau RP Team on Nov. 4, 2021

Hi @allapopovarus,

Bootstrap modals is a third-party library. You will find it (and a couple of things) specified in the “requirements.txt” file, you’ll need to use pip to install them. If you’re curious about the code, you can see it here:

github.com/cltrudeau/django-bstrap-modals

The intent on the SPA stuff was to give a flavor of what you could do, it is always a challenge to jump the gap that in this case includes other languages and libraries.

Feel free to post questions here if you need help with something and I’ll do my best to answer them.

Avatar image for jwil

jwil on Nov. 16, 2024

Well done course. It takes a little while for it to sink in that the DRF can do a LOT of work for you. I think that’s what makes the SPA part a little overwhelming. DRF is still shiny and new, and now here’s a lot of other new toys. I will definitely be revisiting. Thanks again.

Avatar image for Christopher Trudeau

Christopher Trudeau RP Team on Nov. 17, 2024

Hi jwil,

Glad you enjoyed it. My preference these days is to use Django Ninja instead, but the DRF, like Django itself is the workhorse that has been around for a long time and does the job just fine.

Become a Member to join the conversation.