From claude-code-toolkit
Django architecture patterns covering DRF serializers, ORM optimization (select_related, prefetch_related, bulk operations), signals, middleware, and project structure.
How this skill is triggered — by the user, by Claude, or both
Slash command
/claude-code-toolkit:django-patternsThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Organize Django projects with a clear separation between apps, shared utilities, and configuration.
Organize Django projects with a clear separation between apps, shared utilities, and configuration.
project/
config/
settings/
base.py
local.py
production.py
urls.py
wsgi.py
apps/
users/
models.py
serializers.py
views.py
services.py
selectors.py
urls.py
tests/
orders/
...
common/
models.py
permissions.py
pagination.py
Keep business logic in services.py (write operations) and selectors.py (read operations). Views should remain thin.
# select_related for ForeignKey / OneToOne (SQL JOIN)
orders = Order.objects.select_related("customer", "customer__profile").all()
# prefetch_related for ManyToMany / reverse FK (separate query)
authors = Author.objects.prefetch_related(
Prefetch("books", queryset=Book.objects.filter(published=True))
).all()
# Defer fields you don't need
posts = Post.objects.defer("body", "metadata").filter(status="published")
# Use .only() when you need just a few columns
emails = User.objects.only("id", "email").filter(is_active=True)
# Bulk operations
Product.objects.bulk_create(products, batch_size=1000)
Product.objects.bulk_update(products, ["price", "stock"], batch_size=1000)
Always check queries with django-debug-toolbar or connection.queries in tests.
class OrderSerializer(serializers.ModelSerializer):
customer_name = serializers.CharField(source="customer.full_name", read_only=True)
items = OrderItemSerializer(many=True, read_only=True)
total = serializers.SerializerMethodField()
class Meta:
model = Order
fields = ["id", "customer_name", "items", "total", "created_at"]
read_only_fields = ["id", "created_at"]
def get_total(self, obj):
return sum(item.price * item.quantity for item in obj.items.all())
def validate(self, data):
if data.get("start_date") and data.get("end_date"):
if data["start_date"] >= data["end_date"]:
raise serializers.ValidationError("end_date must be after start_date")
return data
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=Order)
def order_created_handler(sender, instance, created, **kwargs):
if created:
send_order_confirmation.delay(instance.id)
update_inventory.delay(instance.id)
Prefer signals for cross-app side effects. For same-app logic, call services directly.
import time
import logging
logger = logging.getLogger(__name__)
class RequestTimingMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
start = time.monotonic()
response = self.get_response(request)
duration = time.monotonic() - start
logger.info(f"{request.method} {request.path} {response.status_code} {duration:.3f}s")
return response
Model.objects.all() without pagination in list endpointsselect_related / prefetch_relatedsettings.py instead of environment variablesselect_related or prefetch_related where neededvalidate methodsnpx claudepluginhub rohitg00/awesome-claude-code-toolkitProvides production-grade Django architecture patterns including project structure, split settings, REST API design with DRF, ORM best practices, caching, signals, and middleware.
Provides Django architecture patterns for project structure, DRF REST APIs, ORM best practices, caching, signals, middleware, and production setups.
Guides Django and FastAPI development: project structure, DRF serializers/viewsets, Pydantic, async ASGI support, admin customization, ORM optimization, deployment.