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

Sneaky REST APIs With Django Ninja (Summary)

In this course, you’ve learned all about the Django Ninja REST API library.

With Ninja, you can:

  • Quickly wrap Django views with a decorator to create REST API endpoints
  • Use type annotations to define variables and arguments
  • Write Schema and ModelSchema classes to encapsulate your serialization needs
  • Enforce authentication on your API
  • Customize errors and exception handling

For more information on the topics you’ve covered in this course, check out:

Download

Sample Code (.zip)

28.3 KB
Download

Course Slides (.pdf)

976.1 KB

00:00 In the previous lesson, I showed you how to write your own custom error handling for Ninja. This lesson is the course summary, and I’ll also point you at some other features of Ninja you might be interested in digging into.

00:13 The Django Ninja package helps you build REST APIs. It is heavily inspired by FastAPI and uses decorators in a similar fashion. The decorators correspond to the HTTP operations you wish to use in your API endpoints.

00:28 Ninja uses Pydantic and type annotations to support quick conversions from HTTP land, where everything is a string, to Python land, where you need more data types.

00:39 The Schema and ModelSchema classes provide serialization support and help you define the response payloads as well as the request bodies in PUTs, PATCHes, and POSTs. ModelSchema is particularly handy, as it means very little coding if all you’re trying to do is serialize a Django ORM model.

00:58 Ninja supports several ways of specifying arguments to your endpoints, including Django-esque path arguments and query strings. In this course, you saw two different authentication mechanisms, and Ninja supports many more.

01:12 You can also write your own truthy functions, allowing or denying as you see fit. And in the previous lesson, I showed you how Ninja catches exceptions and how you can register your own handlers if you want to customize how the errors are processed.

01:29 In addition to everything in this course, Ninja still has more. It fully supports async calls, so if you’re using Uvicorn or Daphne to do ASGI, Ninja’s got your back.

01:40 And as the last couple of Django releases have added more async support, soon enough you’ll be able to do all this with just the Django dev server. Ninja also has a Form class, which you can combine with Schema to handle form-style submissions in your API.

01:56 And if forms aren’t enough, there are features for dealing with file uploads and things like pagination.

02:05 Ninja’s actively being developed, and features are being added frequently. The documentation includes an Enhancement Proposals section if you’d like to see what they’re thinking about or propose something yourself.

02:17 One of the ideas being kicked around at the moment is a class that implements most of the operations in a single place. This is similar to how the Django REST Framework does things.

02:29 The Ninja docs are actually pretty well written. Go digging around in there if you want some more info. Or if you’d like to learn more about REST, the Wikipedia page is a good place to start.

02:40 Ninja’s grandpa is the Django REST Framework. Personally, I find Ninja far easier to use, but the DRF is definitely deeper. Check it out, if nothing else just to see the differences in how they were created.

02:56 For more courses and tutorials in this space, you could check out Building HTTP APIs with Django REST Framework, Python and REST APIs: Interacting With Web Services, a tutorial, and Test-Driven Development of a RESTful API to do a little bit of testing and a little bit of REST API mixed in.

03:19 I hope you had fun with Django Ninja. Look out for the shirouken, though. Ninjas are sneaky.

Become a Member to join the conversation.