Django — Advanced (50+ Coding Exercises)
- Implement an idempotency key model and middleware to enforce idempotent POST requests.
- Implement
F()-expression based atomic counter increment endpoint. - Implement a wallet transfer flow with
transaction.atomic()and DB constraints to prevent double-spend. - Add optimistic locking using a
versioninteger field; reject updates when version mismatches. - Add pessimistic locking using
select_for_update()in a critical section. - Implement object-level permissions for
Projectmodel (owner + collaborators). - Implement a custom auth backend that authenticates via API token header.
- Implement a custom user model using email as username; migrate from default (new project approach).
- Implement a multi-tenant middleware that selects tenant via subdomain and stores it in request.
- Implement tenant-aware QuerySet filtering via custom model manager.
- Implement database router skeleton for multiple DBs (reads to replica, writes to primary).
- Implement a zero-downtime migration plan for adding non-null column (two-step migration + backfill command).
- Implement a data backfill management command with progress output and batching.
- Implement a generic comments system using
contenttypesandGenericForeignKey. - Implement a tagging system reusable across models (generic M2M via through model).
- Implement deep
prefetch_relatedoptimization for a complex page and assert query count in tests. - Implement a cache invalidation strategy: delete relevant cache keys on model save/delete.
- Implement stampede protection for expensive cache fills using a lock key in cache.
- Implement CSP header injection middleware with configurable policy.
- Implement secure file upload validation (extension + content sniffing) and store outside web root.
- Implement direct-to-S3 upload design: create signed URL endpoint and callback verification (code skeleton).
- Implement login rate limiting using cache counters and exponential backoff lockouts.
- Implement request tracing: add request ID middleware + structured logging format.
- Implement metrics endpoint (simple counters) and measure request latency buckets (code skeleton).
- Implement Celery task skeleton with retries and dead-letter handling strategy.
- Implement periodic task schedule configuration (Celery beat or crontab-like).
- Implement a real-time notifications proof-of-concept using Django Channels (routing + consumer skeleton).
- Implement Server-Sent Events endpoint for streaming progress of a long task (ASGI).
- Implement graceful shutdown handling for long-running requests (timeouts + cancellation awareness).
- Implement an audit log model that records create/update/delete for important models.
- Implement an admin interface for audit logs with filters and search.
- Implement “soft delete” across multiple models using an abstract base model + manager.
- Implement GDPR-like data export for a user (collect related models into JSON).
- Implement GDPR-like delete/anonymize job for a user (background task).
- Implement a plugin-style “service layer” pattern for side effects instead of signals.
- Refactor an existing signal side-effect into a service function and update call sites.
- Implement per-object access checks in class-based views with a reusable mixin.
- Implement DRY API error format for JSON responses (custom exception + handler).
- Implement request/response logging with PII redaction (email, token) middleware.
- Implement a safe settings loader reading secrets from env and failing fast if missing.
- Implement a health check endpoint that verifies DB and cache connectivity.
- Implement a readiness endpoint for Kubernetes style deployments (db migrations applied check).
- Implement blue/green deploy checklist as a management command that runs validations.
- Implement database query profiling toggle that logs slow queries above threshold.
- Implement a “maintenance mode” middleware that serves 503 except for admins.
- Implement a file cleanup cron/command that removes orphaned media files.
- Implement a robust pagination API with cursor-based pagination.
- Implement a queryset permission filter that restricts rows based on user role and tenant.
- Implement integration tests for multi-tenant routing and permissions.
- Implement load-test friendly endpoint behavior (timeouts, limits) and add tests for max page size.
- Implement a safe bulk update API that validates fields and uses
bulk_update. - Implement a “rebuild search index” command for a simple text index table.