django-patterns
Learn proven Django patterns for building scalable applications, from organizing projects and optimizing database queries to structuring business logic with services and selectors. Covers Django REST Framework serializers, signals for cross-app effects, custom middleware, and common pitfalls to avoid.
Django Patterns teaches architecture best practices including project organization, ORM optimization, and REST API design.
AI-generated summary based on this skill's SKILL.md
Install
rohitg00/awesome-claude-code-toolkit/django-patterns · repository language: JavaScript
git clone https://github.com/rohitg00/awesome-claude-code-toolkit
cp -r awesome-claude-code-toolkit/skills/django-patterns ~/.claude/skills/django-patternsnpx skillfed install rohitg00/awesome-claude-code-toolkit/django-patternsFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How do I structure Django projects following best practices architecture?
django-patterns teaches proven project organization approaches that scale. Structure your Django project with clear separation between settings (base, local, production), apps organized by feature or domain, and dedicated directories for templates, static files, and utilities. Follow conventions like placing business logic in service layers rather than models, using custom managers for complex queries, and organizing middleware for cross-cutting concerns. The skill covers layout conventions and structural patterns that prevent technical debt.
What's the best way to optimize Django ORM queries and prevent N+1 issues?
django-patterns addresses N+1 query problems through select_related and prefetch_related optimization. Use select_related() for forward foreign keys and one-to-one relationships to fetch related objects in a single query. Apply prefetch_related() for reverse foreign keys and many-to-many relationships. The skill teaches when to use each, how to combine them, and bulk operations for batch inserts. Understanding query optimization prevents performance degradation as your Django application scales.
How should I build REST APIs using Django REST Framework serializers?
django-patterns covers DRF serializer patterns for robust API development. Learn serializer validation techniques, nested serializers for complex data structures, and custom validation methods. The skill teaches how to structure serializers for different endpoints, handle relationships properly, and implement validation at field and object levels. Proper serializer patterns ensure your REST APIs validate input correctly and maintain data integrity across requests.
What are django signals and how do I use them for cross-app communication?
django-patterns explains signals as a mechanism for loose coupling between Django apps. Signals allow one app to notify others of events (like model saves) without direct imports. The skill covers signal patterns for cross-app effects, best practices for signal handlers, and when signals are appropriate versus when service layers are better. Proper signal usage keeps your codebase modular while avoiding circular dependencies.
What common Django anti-patterns should I avoid in my applications?
django-patterns provides a code organization checklist highlighting anti-patterns to avoid. Common pitfalls include putting business logic directly in views, overusing signals for everything, ignoring query optimization, and poor settings configuration. The skill teaches why these patterns cause problems and what to do instead—using service layers, middleware for cross-cutting concerns, and proper separation of concerns. Following these guidelines keeps your Django codebase maintainable and performant.
How do I implement custom middleware and service layers in Django?
django-patterns covers middleware and service layer implementation patterns. Custom middleware intercepts requests/responses for logging, authentication, or request modification. Service layers encapsulate business logic, making it reusable across views and APIs. The skill teaches when to use each pattern, how to structure them properly, and examples of real-world implementations. These patterns improve code organization and make testing easier.
SKILL.md
rendered from the published skill — quoted content, verbatim
Django Patterns
Project Structure
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.
ORM Optimization
```python
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")
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 1 file
skills/django-patterns/SKILL.md