django

Django signals for decoupled event handling

Signals allow different parts of the application to respond to model events without tight coupling. I use post_save for actions after an object is created or updated, like sending notifications or updating related records. The @receiver decorator is c

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 email with HTML templates

I send HTML emails using Django templates for consistent branding. The EmailMultiAlternatives class supports both plain text and HTML versions. I render templates with render_to_string and context data. For transactional emails, I queue them via Celer

Django custom user model with email authentication

Using email instead of username for authentication is a common requirement. I extend AbstractBaseUser and create a custom user manager early in the project because switching later means complex data migrations. The key is setting USERNAME_FIELD = 'ema

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 atomic transactions for data integrity

The atomic decorator/context manager ensures all-or-nothing database operations. I wrap related operations in @transaction.atomic or with transaction.atomic() blocks. If any operation fails, the entire transaction rolls back. This prevents partial dat

Django generic views for CRUD operations

Generic class-based views reduce boilerplate for standard CRUD operations. I use ListView, DetailView, CreateView, UpdateView, and DeleteView. Setting model and template_name is often sufficient. For create/update views, I specify fields or form_class

Django settings organization with environment-based configs

I organize settings into base, development, and production modules. The base contains common settings, while dev and prod override specific values. I use environment variables for secrets via os.environ.get() or python-decouple. The django-environ pac

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 middleware for API versioning

API versioning via middleware provides clean URL routing. I extract version from Accept header or URL prefix and set it on the request object. Views can check request.api_version to return appropriate responses. For breaking changes, I maintain separa

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 JSON field for flexible schema data

JSONField stores structured data without creating separate tables. I use it for settings, metadata, or varying attributes. Django provides database-level JSON operations via lookups like __contains, __has_key. For PostgreSQL, I get native JSON operato