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 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 raw SQL queries for complex operations

For queries too complex for the ORM, I use raw SQL. The raw() method returns model instances. I use cursor.execute() for non-model queries. I always use parameterized queries to prevent SQL injection—never string interpolation. For reporting, raw SQL

Django database index strategies

Indexes dramatically improve query performance. I add indexes via Meta.indexes or db_index=True on frequently-filtered fields. Compound indexes help queries filtering on multiple fields. For text search, I use GinIndex on PostgreSQL. I index foreign k

Django debug toolbar for development

Django Debug Toolbar reveals query counts, cache hits, and template rendering time. I add it only in development settings. The toolbar shows all SQL queries which helps identify N+1 problems. I use the profiler panel to find slow code. The cache panel

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 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

Django timezone-aware datetime handling

Django stores datetimes as UTC in the database when USE_TZ=True. I use timezone.now() instead of datetime.now() to get aware datetimes. The timezone.localtime() converts UTC to user's timezone for display. For user input, I use timezone.make_aware() t

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 throttling for rate limiting

Throttling prevents API abuse by limiting request rates. DRF provides AnonRateThrottle for anonymous users and UserRateThrottle for authenticated users. I configure rates in settings like 'user': '100/hour'. For custom logic, I subclass BaseThrottle a

Django aggregation with annotate for statistics

Aggregation performs database-level calculations efficiently. I use aggregate() for single results across entire queryset (like average or total) and annotate() to add calculated fields to each object. Common aggregates include Count, Sum, Avg, Min, a

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