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 REST Framework pagination with custom classes

I use PageNumberPagination for simple, bookmark-friendly pagination and CursorPagination when data changes frequently (prevents duplicate/missing items between pages). Creating a custom pagination class lets me control page_size, page_size_query_param

Django model managers for custom querysets

Custom managers encapsulate common query patterns. I create a manager method for each frequently-used filter like published() or active(). By returning self, I can chain manager methods. For more complex queries, I create a custom QuerySet class and u

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

Django class-based view with mixins for reusability

Mixins let me compose view behavior without duplication. LoginRequiredMixin is essential for protecting views. I create custom mixins like UserOwnershipMixin to encapsulate common patterns. Mixin order matters due to Python's MRO—I put Django's mixins

Django middleware for request logging and timing

Custom middleware lets me inspect or modify every request and response. I implement both __init__ (called once at startup) and __call__ (for each request). By storing start time in the request, I can measure total processing time including view and te

Django custom management command for data import

Management commands are perfect for scripts that need Django's ORM and settings. I create them in management/commands/ and extend BaseCommand. The add_arguments method defines CLI options using argparse. I use self.stdout.write with style helpers for

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 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 with custom permissions

I create custom permission classes to encapsulate authorization logic outside of views. This IsOwnerOrReadOnly pattern is useful for resources where anyone can read but only the owner can modify. By implementing has_object_permission, I can make granu

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

Frame-powered inline “quick view” that falls back to full page

A “quick view” is basically a show page rendered inside a frame. I implement it by adding a turbo_frame_tag 'quick_view' on the index page, and making item links target that frame. If Turbo is disabled or if the response doesn’t include the frame, the