Django Rest Framework (DRF) is a Django framework that offers support for building REST APIs. Like Django, DRF allows you to build your API views with function-based or class-based views.

Although class-based views can be difficult to work with at first, they offer benefits like better code structure, reusability, inheritance, and conciseness.

django’s default api testing interface

Create a Recipe Manager API With Django REST Framework

A recipe manager app is a great way to learn about class-based views in DRF. Features such as adding, deleting, and editing recipes will help you understand how to implement CRUD (Create, Read, Update, Delete) operations. The following steps will teach you how to create a CRUD API.

You can find the code for this guide onGitHub.

Step 1: Install Django REST Framework and Configure Your Project

Step 2: Create a Model for Your Recipe App

Step 3: Create a Serializer for Your App

A serializer is a Django component that helps you convert complex data types, such as your query set, to a format that you can render, like JSON or XML, and vice versa.

To create a serializer, follow these steps:

Step 4: Write a View for the CREATE Operation

you may create class-based views for your app by importing the generic view available in Django. You can read about these views fromDjango’s official documentation. To implement the CREATE operation of CRUD, you should import theCreateAPIView. You should also import your serializer and model:

To implement the CREATE operation, you only need to specify the serializer your view should use. Here’s an example:

With this setup, you can make POST requests to your app.

Step 6: Write Views for the UPDATE and DELETE Operations

To implement the UPDATE and DELETE operations, you need theUpdateAPIViewandDestroyAPIViewrespectively, so import them:

Next, create the views, just as you did before. This time, your views will inherit from theUpdateAPIViewandDestroyAPIView, respectively:

Step 7: Create URLs for Your App

Step 8: Test Your API Endpoints

From your project directory, run the following:

This should start your server, perform some checks, and print a URL you’re able to access it via.

you’re able to now test your API endpoints by navigating to the respective URLs (e.g.,/api/recipes/) and sendingHTTP request methodsfor CRUD operations. You should see a default interface like this:

Instead of using your browser, you cantest your API with Postman.

Practicing DRY While Creating a CRUD API

DRY (Don’t Repeat Yourself) is aprogramming principle you should adoptto improve the quality of your code.

Although the views written above work well, you can avoid a lot of repetition by using theListCreateAPIViewand theRetrieveUpdateDestroyAPIViewgeneric views.

The ListCreateAPIView combines theListAPIViewandCreateAPIView, while the RetrieveUpdateDestroyAPIView combines theRetrieveAPIView,UpdateAPIView, and theDestroyAPIView.

You can modify your previous views to look like this:

This approach reduces the overall amount of code.

You can create URLs for the new views like this:

You can test these endpoints with Postman or anyAPI testing toolyou prefer.

Generic Class-Based Views Make Your Work Easier

As seen above, generic class-based views can speed up the process of creating views. Now you only need to inherit the right APIView for your use case.

You should also ensure you adopt good programming practices, so you don’t end up writing bad code.