Sunday 26 April 2015

Django REST Framework - CRUD

This article is in continuation with Starting with Django REST Framework which just describes how to list that data using generics.ListAPIView. Now we will learn the simplest and easiest way to augment the same application to have a complete CRUD (Create-Retrieve-Update-Delete) functionality implemented in simplest way possible. This posts just covers the changes made to the previously developed application (click here).

Updating view.py

In view.py, we will create a mixin class for each object (book, author) and then used the same to create specific views of respective objects.

E.g. Here we created BookMixin, and then it is used in multiple inheritance fashion with BookList and BookDetails class.

BookList class also inherits generics.ListCreateAPIView which abstracts the logic for listing all the books or creating new book.

Similarly BookDetails class inherits generics.RetrieveUpdateDestroyAPIView which abstracts methods for retrieving a particular book, updating and deleting it.

from django.shortcuts import render
from library.models import Book, Author
from rest_framework import generics
from library.serializers import BookSerializer, AuthorSerializer


class BookMixin:
    serializer_class = BookSerializer
    queryset = Book.objects.all()


class BookList(BookMixin, generics.ListCreateAPIView):
    """
    Returns list of all books or create a new book
    """
    pass


class BookDetails(BookMixin, generics.RetrieveUpdateDestroyAPIView):
    """
    Returns list of all books or create a new book
    """
    pass


class AuthorMixin:
    serializer_class = AuthorSerializer
    queryset = Author.objects.all()


class AuthorList(AuthorMixin, generics.ListCreateAPIView):
    """
    Returns list of all authors or create new authors
    """
    pass


class AuthorDetails(AuthorMixin, generics.RetrieveUpdateDestroyAPIView):
    """
    Returns a specific author, updates it or deletes it.
    """
    pass

Updating urls.py

Following shows building appropriate URLs for the views. The view class inheriting RetrieveUpdateDestroyAPIView  must be passed a parameter pk (primary key).

from django.conf.urls import url
from library.views import BookList, AuthorList, BookDetails, AuthorDetails

urlpatterns = (
    url(r'^books/$', BookList.as_view(), name="book-list"),
    url(r'^books/(?P<pk>[0-9]+)$', BookDetails.as_view(), name="book-details"),
    url(r'^authors/$', AuthorList.as_view(), name="author-list"),
    url(r'^authors/(?P<pk>[0-9]+)$', AuthorDetails.as_view(), name="author-details"),
)

Following are example screen caps.

Get All Authors using GET method

Get Specific Author using GET method and author primary key

Create Author Entry - 1

Create Author Entry - 2 (POST method)
Updating Author Details using PUT method
Deleting Author Entry using DELETE method

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.

Do you like this article?