Django REST API - CRUD with DRF

Django REST API - CRUD with DRF

Django REST Framework (DRF) is a powerful toolkit for building Web APIs in Django. One of its key features is to allow for rapid development of CRUD (Create, Read, Update, Delete) operations for your data models.

Let's go through a basic example of setting up CRUD operations with DRF:

1. Setup:

First, install Django and DRF:

pip install django djangorestframework 

2. Create a new Django project and app:

django-admin startproject myproject cd myproject python manage.py startapp myapp 

3. Define a model:

In myapp/models.py:

from django.db import models class Item(models.Model): name = models.CharField(max_length=100) description = models.TextField() def __str__(self): return self.name 

Run migrations:

python manage.py makemigrations python manage.py migrate 

4. Setup DRF:

In myproject/settings.py, add 'rest_framework' and 'myapp' to INSTALLED_APPS.

5. Create a Serializer:

Serializers allow complex data types, such as Django models, to be converted to Python data types.

In myapp/serializers.py:

from rest_framework import serializers from .models import Item class ItemSerializer(serializers.ModelSerializer): class Meta: model = Item fields = ['id', 'name', 'description'] 

6. Create a View:

Using DRF's viewsets.ModelViewSet, you can easily set up CRUD operations.

In myapp/views.py:

from rest_framework import viewsets from .models import Item from .serializers import ItemSerializer class ItemViewSet(viewsets.ModelViewSet): queryset = Item.objects.all() serializer_class = ItemSerializer 

7. Create URLs:

DRF provides a DefaultRouter that automatically sets up the CRUD URLs for your viewsets.

In myapp/urls.py:

from django.urls import path, include from rest_framework.routers import DefaultRouter from .views import ItemViewSet router = DefaultRouter() router.register(r'items', ItemViewSet) urlpatterns = [ path('', include(router.urls)), ] 

And in myproject/urls.py:

from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('api/', include('myapp.urls')), ] 

8. Run the Server:

python manage.py runserver 

Now, you should have a fully functioning CRUD API for the Item model at http://localhost:8000/api/items/.

With this setup, you can:

  • Create a new item with POST to /api/items/
  • Read all items with GET from /api/items/
  • Read a single item with GET from /api/items/<id>/
  • Update an item with PUT to /api/items/<id>/
  • Delete an item with DELETE to /api/items/<id>/

This is just a basic introduction, and DRF offers many more features like authentication, permissions, pagination, and more. It's recommended to explore the official DRF documentation for a more comprehensive understanding.


More Tags

navigation android-browser case-conversion pygame-clock ios11 winapi interpolation floating-action-button formulas window-resize

More Programming Guides

Other Guides

More Programming Examples