Django Rest API – From Scratch – Part 2

django rest framework logo

Let’s serialize our own models

This is the continuation of part 1 of my tutorial series. So, if you haven’t viewed part 1 of the tutorial, then you can find it here:


We have already created our todo app, hence we need to register it in our file.


Creating own model

Now, we have to define our model which resides in our database. In your todo/ paste the following lines of code:

from django.db import models

# Create your models here.

class Todo(models.Model):
    title = models.CharField(max_length=100, help_text='Enter the title of your Todo, try to make it short')
    description = models.CharField(max_length=255, help_text='Enter what is the todo about and its details')
    is_completed = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['created']

    def __str__(self):
        return self.title

Finally, you have to migrate the class to the database. You can do it by following lines of code

python makemigrations
python migrate


We need serializers to get the required json. So, we need to create serializers classes. Therefore, add some lines of code in todo/ file

from rest_framework import serializers
from .models import Todo

class TodoSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Todo
        fields = ['title', 'description', 'is_completed', 'created', 'last_modified']


Let’s write some API views using our new Serializer class. So, open todo/ file

Since, we don’t have csrf_token in the client apps from where we will POST, therefore, we have to override the csrf_token. Hence, @csrf_exempt decorator can be used for this purpose, however, its implementation is different for class.

# from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
# from rest_framework.parsers import JSONParser
from .models import Todo
from .serializers import TodoSerializer

class TodoViewSet(viewsets.ModelViewSet):
       API endpoint that allows todos to be viewed or edited.

    def dispatch(self, request, *args, **kwargs):
        return super(TodoViewSet, self).dispatch(request, *args, **kwargs)

    queryset = Todo.objects.all()
    serializer_class = TodoSerializer


Now, we need to wire the views to our URLs. For that, open tutorial/ file and addsome lines of code

from todo import views
router.register(r'todos', views.TodoViewSet)


Finally, you can test the endpoints using Postman. For that, just paste the code below in your terminal.

You have to add a Content-Type to the value application/json. The paste following json in the body and POST it into and Note the trailing slash.

    "title": "My Todo Title",
    "description": "My Todo Description"

Now, you can refresh Here, you will find your todo posted.

To view individual todo, enter the todo id like below


Up to this point, what we did can be summarized as:

  1. Registered the todo app.
  2. Created own model Todo and migrated to the database.
  3. Implemented serializers for the model
  4. Provided viewsets for the model
  5. Registered the route
  6. Tested in Postman

I hope you have successfully completed till now. You can revise the points it know if anything has missed.

1 thought on “Django Rest API – From Scratch – Part 2”

Leave a comment