Building an API with Django REST Framework 3: Creating an endpoint with CRUD operations

In Part 1 of this tutorial, we learnt how to start a Django project, create apps within it, and set up the database. In Part 2, we went through Authentication & Authorization, including user login/ registration/ profile update etc.

In this tutorial, we shall create an endpoint called /notes where users can create, read, update, and delete notes.

In the first tutorial, we learnt how to create django apps within your project. Create a new Django app notesapi.

Create new file notesapi/urls.py with following content:

from django.urls import path
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
]

Go to api/settings.py and add this app in the INSTALLED_APPS array

'notesapi',

Furthermore, open the api/urls.py file and add URLs for the notesapi app;

path('notesapi/', include('notesapi.urls')),

1. Model

First we shall create a model that will store the notes data. Go to notesapi/models.py and add the following

from django.db import models

class Notes(models.Model):
title = models.CharField(max_length=255, null=False)
description = models.CharField(max_length=255, null=False)
price = models.CharField(max_length=5, null=False)

def __str__(self):
return "{} - {} - {}".format(self.title, self.description, self.price)

We can also add our model to the admin section to be able to add note instances, so that we can manually test this endpoint. Go ahead add the following lines of code to the notesapi/admin.py file.

from .models import Notes

admin.site.register(Notes)

Now lets make migrations

python3 manage.py makemigrations
python3 manage.py migrate

You can login into admin at http://127.0.0.1:8000/admin using superuser account, and create some notes data at this place.

2. Serializer

The second step is to create a serializer for our data model. Serializers allow data and model instances to be converted into and from JSON, XML, etc, for our requests and responses.

Add a new file notesapi/serializers.py and add the following lines of code;

from .models import Notes
from rest_framework import serializers

class NotesSerializer(serializers.ModelSerializer):
class Meta:
model = Notes
fields = ("title", "description", "price")

3, View

Create a view that returns all notes. Open the notesapi/views.py file and add the following lines of code;

from django.shortcuts import render
from rest_framework import generics
from .models import Notes
from .serializers import NotesSerializer
from rest_framework import viewsets

class NotesViewSet(viewsets.ModelViewSet):

queryset = Notes.objects.all()
serializer_class = NotesSerializer

4. Urls

Open the notesapi/urls.py file and add the following lines of code;

from django.urls import path
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include

from rest_framework import routers
from notesapi.views import NotesViewSet

router = routers.DefaultRouter()
router.register(r'notes', NotesViewSet)

urlpatterns = [
    path(r'', include(router.urls)),
]

That’s it. Now you can run the server, and go to http://127.0.0.1:8000/notesapi/notes/ and test GET/POST etc.

POST /notes will create a new note

GET /notes will return all the notes

GET notes/:id/ will return a single note detail

PUT notes/:id/ will update a single note

DELETE notes/:id/ will delete a note

. . .


Also published on Medium.

By |2019-04-11T06:17:51+00:00April 11th, 2019|Categories: Django|Tags: |0 Comments

Leave A Comment