python

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 caching with cache_page and cache decorator

I use Django's cache framework to avoid expensive queries and computations. The cache_page decorator caches entire view responses by URL. For more control, I use the low-level cache API to store query results or computed values. I set reasonable timeo

Django performance monitoring with django-silk

Django Silk profiles SQL queries, HTTP requests, and Python code. I install it in development to identify bottlenecks. It shows query counts, execution times, and duplicate queries per request. The web UI visualizes performance data. I use @silk_profi

Django email with attachments and templates

Django's email system supports attachments and HTML templates. I use EmailMessage for full control or EmailMultiAlternatives for HTML+text versions. For attachments, I use attach_file() or attach() methods. I render email content from templates for co

Django redis caching strategies

Redis provides fast in-memory caching for Django. I configure it as cache backend and use for session storage. I cache expensive querysets, computed values, and API responses. The cache.get_or_set() method simplifies cache-aside pattern. For cache inv

Django channels for WebSockets and async

Django Channels adds WebSocket support and async capabilities. I define consumers similar to views for handling connections. The channel layer enables communication between consumers. For real-time features like chat or notifications, Channels is esse

Django file upload handling with validation

File uploads require careful validation for security. I validate file size using a custom validator and check content type. Using FileField or ImageField, Django handles storage automatically. I configure MEDIA_ROOT and MEDIA_URL for development. For

Django REST Framework nested routers

Nested routers create hierarchical URL structures for related resources. I use drf-nested-routers to define parent-child relationships in URLs like /posts/1/comments/. This makes APIs more RESTful and intuitive. I filter child resources by parent ID i

Django deployment checklist and production settings

Deploying Django to production requires many configuration changes. I set DEBUG=False and configure ALLOWED_HOSTS. Security settings include SECURE_SSL_REDIRECT, SESSION_COOKIE_SECURE, CSRF_COOKIE_SECURE. I use environment variables for secrets. Stati

Django database routers for multiple databases

Database routers direct queries to specific databases. I implement db_for_read(), db_for_write(), allow_relation(), and allow_migrate() methods. This enables read replicas, sharding by model, or separating analytics data. The router checks model label

Django select_related and prefetch_related for N+1 query optimization

The N+1 query problem is Django's most common performance trap. I use select_related for foreign key and one-to-one relationships (performs SQL JOIN), and prefetch_related for many-to-many and reverse foreign keys (separate queries with Python join).

Django REST Framework viewset actions

Custom actions extend viewsets beyond CRUD operations. I use @action decorator with detail=True/False for object-level or collection-level actions. This creates endpoints like /posts/1/publish/ or /posts/recent/. I specify HTTP methods, permissions, a