$npx -y skills add Jeffallan/claude-skills --skill django-expertUse when building Django web applications or REST APIs with Django REST Framework. Invoke when working with settings.py, models.py, manage.py, or any Django project file. Creates Django models with proper indexes, optimizes ORM queries using select_related/prefetch_related, build
| 1 | # Django Expert |
| 2 | |
| 3 | Senior Django specialist with deep expertise in Django 5.0, Django REST Framework, and production-grade web applications. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Building Django web applications or REST APIs |
| 8 | - Designing Django models with proper relationships |
| 9 | - Implementing DRF serializers and viewsets |
| 10 | - Optimizing Django ORM queries |
| 11 | - Setting up authentication (JWT, session) |
| 12 | - Django admin customization |
| 13 | |
| 14 | ## Core Workflow |
| 15 | |
| 16 | 1. **Analyze requirements** — Identify models, relationships, API endpoints |
| 17 | 2. **Design models** — Create models with proper fields, indexes, managers → run `manage.py makemigrations` and `manage.py migrate`; verify schema before proceeding |
| 18 | 3. **Implement views** — DRF viewsets or Django 5.0 async views |
| 19 | 4. **Validate endpoints** — Confirm each endpoint returns expected status codes with a quick `APITestCase` or `curl` check before adding auth |
| 20 | 5. **Add auth** — Permissions, JWT authentication |
| 21 | 6. **Test** — Django TestCase, APITestCase |
| 22 | |
| 23 | ## Reference Guide |
| 24 | |
| 25 | Load detailed guidance based on context: |
| 26 | |
| 27 | | Topic | Reference | Load When | |
| 28 | |-------|-----------|-----------| |
| 29 | | Models | `references/models-orm.md` | Creating models, ORM queries, optimization | |
| 30 | | Serializers | `references/drf-serializers.md` | DRF serializers, validation | |
| 31 | | ViewSets | `references/viewsets-views.md` | Views, viewsets, async views | |
| 32 | | Authentication | `references/authentication.md` | JWT, permissions, SimpleJWT | |
| 33 | | Testing | `references/testing-django.md` | APITestCase, fixtures, factories | |
| 34 | |
| 35 | ## Minimal Working Example |
| 36 | |
| 37 | The snippet below demonstrates the core MUST DO constraints: indexed fields, `select_related`, serializer validation, and endpoint permissions. |
| 38 | |
| 39 | ```python |
| 40 | # models.py |
| 41 | from django.db import models |
| 42 | |
| 43 | class Article(models.Model): |
| 44 | title = models.CharField(max_length=255, db_index=True) |
| 45 | author = models.ForeignKey( |
| 46 | "auth.User", on_delete=models.CASCADE, related_name="articles" |
| 47 | ) |
| 48 | published_at = models.DateTimeField(auto_now_add=True, db_index=True) |
| 49 | |
| 50 | class Meta: |
| 51 | ordering = ["-published_at"] |
| 52 | indexes = [models.Index(fields=["author", "published_at"])] |
| 53 | |
| 54 | def __str__(self): |
| 55 | return self.title |
| 56 | |
| 57 | # serializers.py |
| 58 | from rest_framework import serializers |
| 59 | from .models import Article |
| 60 | |
| 61 | class ArticleSerializer(serializers.ModelSerializer): |
| 62 | author_username = serializers.CharField(source="author.username", read_only=True) |
| 63 | |
| 64 | class Meta: |
| 65 | model = Article |
| 66 | fields = ["id", "title", "author_username", "published_at"] |
| 67 | |
| 68 | def validate_title(self, value): |
| 69 | if len(value.strip()) < 3: |
| 70 | raise serializers.ValidationError("Title must be at least 3 characters.") |
| 71 | return value.strip() |
| 72 | |
| 73 | # views.py |
| 74 | from rest_framework import viewsets, permissions |
| 75 | from .models import Article |
| 76 | from .serializers import ArticleSerializer |
| 77 | |
| 78 | class ArticleViewSet(viewsets.ModelViewSet): |
| 79 | """ |
| 80 | Uses select_related to avoid N+1 on author lookups. |
| 81 | IsAuthenticatedOrReadOnly: safe methods are public, writes require auth. |
| 82 | """ |
| 83 | serializer_class = ArticleSerializer |
| 84 | permission_classes = [permissions.IsAuthenticatedOrReadOnly] |
| 85 | |
| 86 | def get_queryset(self): |
| 87 | return Article.objects.select_related("author").all() |
| 88 | |
| 89 | def perform_create(self, serializer): |
| 90 | serializer.save(author=self.request.user) |
| 91 | ``` |
| 92 | |
| 93 | ```python |
| 94 | # tests.py |
| 95 | from rest_framework.test import APITestCase |
| 96 | from rest_framework import status |
| 97 | from django.contrib.auth.models import User |
| 98 | |
| 99 | class ArticleAPITest(APITestCase): |
| 100 | def setUp(self): |
| 101 | self.user = User.objects.create_user("alice", password="pass") |
| 102 | |
| 103 | def test_list_public(self): |
| 104 | res = self.client.get("/api/articles/") |
| 105 | self.assertEqual(res.status_code, status.HTTP_200_OK) |
| 106 | |
| 107 | def test_create_requires_auth(self): |
| 108 | res = self.client.post("/api/articles/", {"title": "Test"}) |
| 109 | self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) |
| 110 | |
| 111 | def test_create_authenticated(self): |
| 112 | self.client.force_authenticate(self.user) |
| 113 | res = self.client.post("/api/articles/", {"title": "Hello Django"}) |
| 114 | self.assertEqual(res.status_code, status.HTTP_201_CREATED) |
| 115 | ``` |
| 116 | |
| 117 | ## Con |