django

Django database indexes for query performance

Database indexes dramatically speed up queries. I add indexes to frequently-filtered fields via db_index=True or Meta.indexes. Compound indexes help queries filtering on multiple fields together. For text search on PostgreSQL, I use GinIndex with Sear

Django model property for computed fields

Properties let me add computed attributes to models without storing them in the database. I use @property for simple calculations like full name or age. For expensive computations, I consider caching the result in a field and updating it via signals o

Django REST Framework schema and documentation

DRF auto-generates API schemas and documentation. I use drf-spectacular for OpenAPI 3.0 schemas. The schema describes endpoints, parameters, and responses. I customize with decorators like @extend_schema. Interactive docs via Swagger UI or ReDoc let d

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 inheritance with abstract base classes

Abstract base classes let me define common fields and methods without creating database tables. I set abstract = True in Meta. Concrete models inheriting from the abstract class get all its fields and methods. This is perfect for timestamps, soft dele

Django haystack for advanced search

Haystack provides unified search across backends (Elasticsearch, Solr, Whoosh). I define search indexes mapping models to searchable fields. It handles full-text search, faceting, and highlighting. The SearchQuerySet API is similar to Django's ORM. I

Django test fixtures with factory_boy

Factory Boy eliminates boilerplate in tests by generating model instances with sensible defaults. I use Sequence for unique values, SubFactory for foreign keys, and post_generation for many-to-many relationships. The Faker integration provides realist

Django password reset flow with email

Django provides built-in password reset views. I customize templates to match site design. The flow sends a secure token via email that expires after a timeout. I configure email backend and PASSWORD_RESET_TIMEOUT in settings. For better UX, I customi

Django celery task for async email sending

I use Celery for any operation that might be slow or fail intermittently, like sending emails. By decorating with @shared_task, I make tasks reusable across different apps. I set bind=True to access task instance (useful for retries), and configure re

Django context processors for global template variables

Context processors add variables to every template context automatically. I create a function that takes request and returns a dict. This is perfect for site-wide settings like site name, current year, or user preferences. I add my processor to TEMPLA

Django form validation with clean methods

I use clean_<fieldname>() to validate individual fields and clean() to validate field combinations. Raising ValidationError shows the message to the user near the appropriate field. For cross-field validation (like 'end date must be after start