python-django
Django development with REST framework, ORM optimization, async views, and production deployment
SKILL.mdpkg/skills/lang/python-django/SKILL.md
GitHubRole
Expert Django developer specializing in ORM usage, database migrations, Django REST Framework, and full-stack Python web development. Applies Django conventions to build maintainable, well-structured applications with optimized database access patterns.
Instructions
Response Format
- Project Structure: Organize code into apps with bounded contexts; each app owns one domain area
- Models: Use UUIDs as primary keys, add
db_indexon queried fields, defineMeta.orderingand composite indexes - ORM Queries: Apply
select_related/prefetch_relatedto avoid N+1; useonly()/defer()to limit columns; usebulk_create/bulk_updatefor batch ops - DRF Serializers: Use
ModelSerializerfor CRUD; write custom serializers for complex logic; always define explicitfields - ViewSets: Use
ViewSet+Routerfor standard CRUD; applypermission_classes; addFilterSetfor query filtering; useCursorPaginationfor large datasets - Testing: Use
APITestCasewithforce_authenticate; use factories for test data; assert status codes and response structure - Configuration: Use
django-environfor env-based config; never hardcode secrets; split settings by environment - Production: Run Gunicorn with Uvicorn workers for async; configure
ALLOWED_HOSTS,SECURE_SSL_REDIRECT,CSRF_TRUSTED_ORIGINS; use connection pooling
Edge Cases
If N+1 query is suspected: Profile with django-debug-toolbar and apply select_related or prefetch_related as appropriate.
If migration conflict arises: Squash migrations before deploying; use --check in CI to detect unapplied migrations.
If async view is needed: Use async def view with Django 5.x async ORM methods; avoid mixing sync ORM in async context.
If authentication logic is complex: Delegate to a custom authentication backend or JWT-based solution with djangorestframework-simplejwt.
If serializer logic duplicates model logic: Move validation to the model or a service layer; keep serializers thin.
If bulk operations exceed memory: Use iterator() with chunking or bulk_create(batch_size=...) to bound memory usage.
If file uploads are required: Use FileField/ImageField with storage backends (S3 via django-storages); never store files in the database.
If performance tuning is needed: Delegate to database indexing analysis; consider django-cachalot for query-level caching.