Using Django REST Framework for APIs

Tutorial 2 of 5

1. Introduction

The Django REST framework is a powerful and flexible toolkit for building Web APIs. This tutorial will provide a step-by-step guide to help you understand how to use Django REST framework to build APIs.

By the end of this tutorial, you should be able to:

  • Understand the basic concepts related to Django REST framework
  • Set up a project using Django REST framework
  • Create, retrieve, update, and delete instances of a model via the API

Prerequisites for this tutorial include a basic understanding of Python, Django, and HTTP methods. Familiarity with JSON and RESTful APIs will also be beneficial.

2. Step-by-Step Guide

Django REST framework works on top of Django and its ORM. It provides an easy way to serialize the data into JSON or XML formats and expose these data to be consumed by client-side applications.

Here is a detailed guide for creating a simple API using Django REST framework:

  1. Install Django and Django REST framework: You can install Django and Django REST framework using pip.

pip install django pip install djangorestframework

  1. Create a new Django project and application: Run the following commands to create a new Django project and application.

django-admin startproject myproject cd myproject django-admin startapp myapp

  1. Update settings.py: Add 'rest_framework' and 'myapp' to the INSTALLED_APPS in settings.py.

  2. Create a model in models.py: For example, you can create a simple Book model with fields for title and author.

  3. Create a serializer: In serializers.py, create a BookSerializer with fields for id, title, and author.

  4. Create a view: In views.py, create a BookViewSet that uses the BookSerializer.

  5. Update urls.py: Add a default router and register the BookViewSet with it.

3. Code Examples

Here are some examples to illustrate the steps mentioned above:

  1. Creating a model

```python
from django.db import models

class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
```

  1. Creating a serializer

```python
from rest_framework import serializers
from .models import Book

class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'title', 'author']
```

  1. Creating a view

```python
from rest_framework import viewsets
from .serializers import BookSerializer
from .models import Book

class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
```

  1. Updating urls.py

```python
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import BookViewSet

router = DefaultRouter()
router.register(r'books', BookViewSet)

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

4. Summary

In this tutorial, you learned the basics of using Django REST framework to build APIs. We covered how to install Django and Django REST framework, create a new Django project and application, and create a model, serializer, view, and URL pattern for the API.

5. Practice Exercises

  1. Create a new model called Author with fields for name and bio. Add a foreign key relationship between Book and Author.

  2. Update the serializers and views to include the new Author model.

  3. Test the API by creating, updating, and deleting Author and Book instances.

  4. Explore other features of Django REST framework, such as authentication and permissions.

Remember, the best way to learn is by doing. Happy coding!