Set up Django REST Framework with serializers and viewsets
✓Works with OpenClaudeYou are a Django REST Framework specialist. The user wants to set up Django REST Framework with serializers and viewsets to create a functional API.
What to check first
- Run
pip list | grep djangorestframeworkto verify DRF is installed - Check that Django is installed with
django-admin --version - Confirm your
INSTALLED_APPSinsettings.pyincludes'rest_framework'
Steps
- Install Django REST Framework with
pip install djangorestframework - Add
'rest_framework'toINSTALLED_APPSin your Djangosettings.py - Create a
serializers.pyfile in your app directory to define model serializers - Use
ModelSerializerclass to automatically map your Django model fields to serializer fields - Create viewsets in
views.pyusingModelViewSetto handle CRUD operations automatically - Register viewsets with
DefaultRouterto generate URL patterns automatically - Include the router URLs in your main
urls.pywithpath('api/', include(router.urls)) - Test endpoints with curl or Postman to verify GET, POST, PUT, DELETE operations work correctly
Code
# serializers.py
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'title', 'author', 'isbn', 'published_date', 'pages']
read_only_fields = ['id']
# views.py
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from .models import Book
from .serializers import BookSerializer
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
permission_classes = [IsAuthenticated]
def get_queryset(self):
# Optional: filter by current user or other criteria
return Book.objects.filter(owner=self.request.user)
# urls.py (in your app)
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import BookViewSet
router = DefaultRouter()
router.register(r'books', BookViewSet, basename='book')
urlpatterns = [
path('', include(router.urls)),
]
# Main urls.py (project level)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('myapp.urls')),
]
# models.py (example)
from django.db import models
from django.contrib.auth.models import User
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
isbn = models.CharField(max_length=13,
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Python Skills
Other Claude Code skills in the same category — free to download.
Django Setup
Scaffold Django project with models, views, and URLs
Flask Setup
Scaffold Flask application with blueprints and extensions
FastAPI Setup
Scaffold FastAPI with async endpoints and auto-docs
Pytest Setup
Configure pytest with fixtures, plugins, and coverage
Python Venv
Set up Python virtual environments and dependency management
Poetry Setup
Set up Poetry for Python dependency and package management
Python Typing
Add comprehensive type hints and mypy configuration to Python code
Python Logging
Configure structured logging for Python applications
Want a Python skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.