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

Django CORS configuration for API access

Cross-Origin Resource Sharing (CORS) enables frontend apps on different domains to access your API. I use django-cors-headers for production-ready CORS handling. I configure CORS_ALLOWED_ORIGINS for specific domains in production and use CORS_ALLOW_AL

Django model save override for custom logic

Overriding the save() method allows custom behavior before or after saving. I call super().save() to preserve default behavior. Common use cases include auto-generating slugs, updating related objects, or validating business rules. I use created check

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 logging configuration for production

Proper logging is critical for production debugging. I configure loggers for different apps and libraries. I use file handlers for persistence and console handlers for development. Log rotation prevents disk fill-up. I set appropriate levels (DEBUG, I

Django ORM window functions for analytics

Window functions perform calculations across rows related to the current row. I use them for running totals, rankings, and moving averages. Django's Window expression with functions like RowNumber, Rank, DenseRank provide SQL window function support.

Django session management and custom session backends

Sessions store user state across requests. Django supports database, cache, file, and cookie-based sessions. I use cacheddb for read performance with database persistence. For APIs, I avoid sessions in favor of token auth. The request.session dict-lik

Django URL namespacing and reverse lookups

URL namespaces prevent name collisions across apps. I set app_name in app-level urls.py and use namespace in include(). Reverse lookups use reverse('namespace:name') or {% url 'namespace:name' %} in templates. This makes URLs maintainable when refacto

Django formsets for editing multiple objects

Formsets handle multiple forms on one page. I use modelformset_factory for editing existing objects and inlineformset_factory for related objects. The extra parameter controls empty forms shown. I validate formsets with formset.is_valid() and save wit

Django model soft delete pattern

Soft deletes mark records as deleted without removing them from the database. I add a deleted_at field and override the delete() method. A custom manager excludes soft-deleted records by default. I provide a hard_delete() method for permanent removal.

Django REST Framework authentication with JWT

JWT tokens provide stateless authentication for APIs. I use djangorestframework-simplejwt for token generation and validation. The TokenObtainPairView issues access and refresh tokens on login. I configure token lifetimes in settings. For protected en