Django streaming responses for large files

Streaming responses serve large files without loading them entirely into memory. I use StreamingHttpResponse or FileResponse for file downloads. For CSV generation, I yield rows incrementally. The generator pattern keeps memory usage constant regardle

Django database connection pooling for performance

Connection pooling reuses database connections instead of creating new ones per request. I use django-db-pool or configure persistent connections with CONN_MAX_AGE. This reduces connection overhead significantly. For high-traffic sites, I set appropri

Django custom authentication backend

Custom auth backends enable alternative authentication methods. I subclass ModelBackend and override authenticate(). Common use cases include email login, LDAP, OAuth, or custom token auth. The backend returns a user object or None. I add it to AUTHEN

Django contenttypes framework for generic relations

ContentTypes enable generic foreign keys pointing to any model. I use GenericForeignKey for features like comments, tags, or favorites on multiple content types. The framework tracks all installed models. I query with content_type and object_id. For r

Django message framework for user feedback

Django's message framework provides one-time notifications to users. I use messages.success(), messages.error(), messages.warning(), and messages.info() to add messages. Messages persist across redirects and are displayed once. I configure message sto

Django model validation with clean method

The clean() method validates model instances before saving. I raise ValidationError for invalid data. This runs on form submission and can be called explicitly with full_clean(). I validate cross-field constraints that can't be expressed as field vali

Django sitemap generation for SEO

Sitemaps help search engines discover and index pages. Django's sitemap framework generates XML sitemaps automatically. I create sitemap classes for each content type, defining items(), lastmod(), changefreq(), and priority(). For large sites, I use s

Django background tasks with Celery beat for scheduling

Celery Beat schedules periodic tasks like cron jobs. I define schedules in settings or use the Django database scheduler. Tasks run at specified intervals or cron expressions. I use @periodic_task decorator or configure in CELERY_BEAT_SCHEDULE. For dy

Django select_for_update for database locking

select_for_update() locks rows until transaction completes, preventing race conditions. I use it for operations requiring read-modify-write atomicity like decrementing stock or updating counters. The lock is released on transaction commit/rollback. Fo

Django model meta options for database optimization

Model Meta class controls database behavior. I use ordering for default query ordering, indexes for database indexes, and unique_together for composite uniqueness constraints. The db_table option customizes table names. For large tables, managed=False

Django REST Framework filtering with django-filter

django-filter provides declarative filtering for DRF viewsets. I define a FilterSet class with fields to filter on. The DjangoFilterBackend integrates seamlessly with DRF. I use CharFilter, NumberFilter, DateFilter etc. for different field types. Look

Django custom decorators for view logic

Custom decorators encapsulate reusable view logic. I use functools.wraps to preserve function metadata. For class-based views, I use method_decorator. Common patterns include permission checks, rate limiting, or request validation. Decorators can modi