Create a Restful API (CRUD) using the Django Rest Framework

A Rest API is a fundamental component in any application nowadays that offers us a simple, organized and customizable mechanism to share data or functionalities through other applications; It even allows us to add security layers with relative ease if you use a framework, package or something like that; In Django, using the Django Rest Framework cannot be the exception and allows us to create highly customized Django RestApis through classes or methods that we can customize.

So again, there are many ways to create Rest resources using the Django Rest Framework (DRF) and in this post we're going to look at the simplest, most generated, and most straightforward approach to learning how to get started with DRF.

Components to create a Rest Api in DRF

In DRF there are 3 main components to create any type of Rest resource:

  1. The serialization classes, which allow us to indicate the data set with which we are going to work.
  2. The classes or methods with the Rest resources, which the serializers use to map to the responses of the Rest resources we created at this point.
  3. Routing, like any http component in Django, we need to create a route component.

And all this we can manage through classes, properties and functions in DRF.

Defining the example model

We are going to work with a simple category model like the following:

from django.db import models class Category(models.Model):    
  title = models.CharField(max_length=255)    
  url_clean = models.CharField(max_length=255)    
def __str__(self):        
   return self.title

Creating the Restful API

As we mentioned initially, we are going to create a CRUD Restful Api, or Restful Api, therefore we are going to use the corresponding scheme that allows us to create this type of class using the 3 components explained above:

Views to define the Rest resources

If your intention is to create a CRUD Rest API, there is already a class in DRF that you can inherit and it will be created automatically thanks to the two attributes that we define, the queryset, to define the data pull, and the serializer_class to define the serializer class; we will do all this in a file called viewsets.py:

from rest_framework import viewsets 
from .models import Category 
from .serializers import CategorySerializer 
class CategoryViewSet(viewsets.ModelViewSet):    
  queryset = Category.objects.all()    
  serializer_class = CategorySerializer

The first class that we have to define would be serializers.py that allows us to define the serialization of our classes, this is useful to indicate which fields we are going to work with, for example, all:

from rest_framework import serializers 
from .models import Category 
class CategorySerializer(serializers.ModelSerializer):    
  class Meta:        
    model = Category        
    fields = '__all__'

With our two resources created, the next thing we need would be handling the routes; for that:

from rest_framework import routers 
from .viewsets import CategoryViewset 
route = routers.SimpleRouter() 
route.register('category',CategoryViewset) 
urlpatterns = route.urls

Then in our main file for the routes:

path('api/', include('restful.urls')),

Where restful is the name of the application that contains the above classes.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 3d 09:01!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!