skillfed

django-drf

Learn proven Django REST Framework patterns for building scalable APIs. This skill covers ViewSets, Serializers for different operations, filtering strategies, custom permissions, pagination setup, and routing conventions. Includes testing patterns and common management commands.

django-drf teaches you Django REST Framework patterns for building production APIs with ViewSets, Serializers, and Filters.

AI-generated summary based on this skill's SKILL.md

604 88 MIT updated by Gentleman-Programming

Install

Gentleman-Programming/Gentleman-Skills/django-drf

git clone https://github.com/Gentleman-Programming/Gentleman-Skills
cp -r Gentleman-Skills/curated/django-drf ~/.claude/skills/django-drf
npx skillfed install Gentleman-Programming/Gentleman-Skills/django-drf

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

What are django-drf ViewSet examples and how do I use them?

django-drf ViewSets combine related views into a single class, reducing boilerplate. ModelViewSet provides full CRUD operations automatically. Basic example: inherit from ModelViewSet, set queryset and serializer_class, then register with a router. ViewSets handle list, create, retrieve, update, and destroy actions. Use @action decorator for custom endpoints. This pattern is central to building production APIs efficiently.

How do I build a REST API with Django and django-drf?

django-drf simplifies REST API development through ViewSets, Serializers, and routers. Define models, create Serializers to handle data validation and transformation, build ViewSets to handle HTTP requests, then register routes with DefaultRouter. Add filtering, permissions, and pagination in settings. Use token or JWT authentication for security. Test endpoints with pytest or the browsable API. This structured approach ensures scalable, maintainable APIs.

What are the key django-drf serializer patterns for different operations?

django-drf supports multiple serializer patterns: use one serializer for list/retrieve and another for create/update operations, implement SerializerMethodField for computed properties, set read_only_fields for output-only data, and use nested serializers for relationships. Create separate serializers when input validation differs from output format. Override to_representation() and to_internal_value() for custom logic. These patterns ensure clean separation of concerns and robust validation.

How do I set up filtering, permissions, and pagination in django-drf?

django-drf filtering uses DjangoFilterBackend with filterset_fields or custom FilterSet classes. Permissions are applied via permission_classes on ViewSets—use IsAuthenticated, IsAdminUser, or custom classes like IsOwner. Pagination is configured globally in settings with DEFAULT_PAGINATION_CLASS and PAGE_SIZE, or per-ViewSet. Combine these in ViewSet: set filter_backends, permission_classes, and pagination_class. This ensures secure, scalable APIs with controlled data access.

How do I test django-drf endpoints and API logic effectively?

django-drf testing uses APITestCase or pytest fixtures to create test clients and authenticate users. Test each ViewSet action: list, create, retrieve, update, destroy. Verify status codes, response data, and side effects. Use factories for test data, mock external services, and test permissions by making requests as different users. Test serializer validation separately. This comprehensive approach catches bugs early and ensures API reliability.

What is the difference between django-drf ModelViewSet and ViewSet?

django-drf ViewSet is a base class requiring manual action definition; ModelViewSet extends it with automatic CRUD actions (list, create, retrieve, update, destroy, partial_update). Use ModelViewSet for standard database operations, ViewSet for custom logic or non-database endpoints. Both work with routers for automatic URL generation. Choose based on your needs: ModelViewSet saves code for typical resources, ViewSet offers flexibility for complex scenarios.

SKILL.md

rendered from the published skill — quoted content, verbatim

ViewSet Pattern

from rest_framework import viewsets, status
from rest_framework.response import Response
from rest_framework.decorators import action

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filterset_class = UserFilter
    permission_classes = [IsAuthenticated]

    def get_serializer_class(self):
        if self.action == "create":
            return UserCreateSerializer
        if self.action in ["update", "partial_update"]:
            return UserUpdateSerializer
        return UserSerializer

    @action(detail=True, methods=["post"])
    def activate(self, request, pk=None):
        user = self.get_object()
        user.is_active = True
        user.save()
        return Response({"status": "activated"})

Serializer Patterns

```python from rest_framework import serializers

Read Serializer

class UserSerializer(serializers.ModelSerializer):

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 1 file
curated/django-drf/SKILL.md

Related skills

Tags

api-development rest-patterns backend-framework request-handling data-validation access-control test-automation query-filtering response-formatting endpoint-routing