RESTful API in Django [Django Rest Framework]
Tags: django, python, codenewbie, programming
Published: 2024-05-15 18:21:12

Mastering Django REST Framework: A Guide to Building RESTful APIs
By @bekbrace aka. Amir Bekhit

Hey there, Python developers [and other fellow devs]. 
If you're diving into web development with Django, adding the Django REST Framework (DRF) to your toolkit will be a game-changer. 
It makes building RESTful APIs straightforward and efficient. 
In this post, I'll introduce you to RESTful APIs and how to use DRF to build a simple yet powerful library management system (I love books!) 
Plus, I've included a video where I demonstrate coding examples to create, edit, delete, and view books.
# The Video Guide
Check out my video, where I demonstrate these steps in more detail. You'll see how to create a full CRUD API to manage books, complete with authentication and a browsable UI for testing - if you don't want to watch, continue reading then :)

{%youtube KYKDMeAg3TA%}

# What is a RESTful API?
RESTful APIs (Representational State Transfer) adhere to architectural principles that ensure flexibility and scalability. The primary idea is to access resources via standard HTTP methods like GET, POST, PUT, and DELETE. Each resource is accessed via a unique URL endpoint.

# What is Django REST Framework?
Django REST Framework is a toolkit that seamlessly integrates with Django to build web APIs. Its key features include:

Serialization: Convert data between Python objects and JSON.
Authentication & Permissions: Support for custom and built-in authentication.
Class-Based Views: Reusable views with method-based HTTP request handling.
Browsable API: An interactive UI for testing APIs directly in the browser.

## Getting Started with DRF

# Install DRF:
Install Django REST Framework with pip.

```bash
pip install djangorestframework
```

# Add to Installed Apps:
Update your settings.py file to include 'rest_framework' in the INSTALLED_APPS section.

```python
INSTALLED_APPS = [
    # other apps
    'rest_framework',
]
```

# Build Your Library Management API
Models: Start by defining the book model.

```python
# models.py
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    published_date = models.DateField()
    isbn = models.CharField(max_length=13, unique=True)
Serializers: Create a serializer to convert the book model to and from JSON.
```

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

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = '__all__'
Views: Implement views using DRF’s class-based views.
```

```python
# views.py
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer

class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
URLs: Finally, register the viewsets in your URL configuration.
```

```python
# urls.py
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('api/', include(router.urls)),
]
```
That is it guys, check out the results using CURL [if you're on mac or linux, or you can download git bash on windows] => Check the video for better demonstration.

Conclusion
With the Django REST Framework, building APIs is a awesome (as i live always to say :) ). 
I hope this guide gives you a strong foundation to get started. Got questions? Drop them in the comments!

𝕏:  https:www.twitter.com/bekbrace  
IG: https://www.instagram.com/bek_brace