django

Django bulk operations for performance

Bulk operations reduce database round-trips dramatically. I use bulk_create() for inserting many objects at once, bulk_update() for updates, and update() for queryset-level updates. These bypass save() methods and signals for speed. For large datasets

Django REST Framework nested serializers with writable fields

Nested serializers display related data clearly but are read-only by default. To make them writable, I override create() and update() methods. For simple nesting, PrimaryKeyRelatedField or SlugRelatedField works well. For deeper nesting, I validate ne

Django model inheritance with proxy models

Proxy models create different Python behavior for the same database table. I set proxy = True in Meta. This adds methods or changes default ordering without new tables. Proxy models share the same table as the original. Use cases include different man

Django database migrations best practices

Migrations track database schema changes. I use makemigrations after model changes and review generated migrations. For data migrations, I create empty migrations with makemigrations --empty and write RunPython operations. I test migrations on dev dat

Django database read-write split with routers

Database routers enable read-write splitting for scaling. I direct reads to replicas and writes to primary. The router examines hints and model to make routing decisions. For read-heavy apps, this distributes load across multiple databases. I use usin

Django query optimization with only and defer

The only() method loads specified fields only, while defer() excludes specified fields. This reduces data transfer and memory usage. I use only() when I need just a few fields from large models. For read-only displays, this prevents loading unnecessar

Django database connection pooling for performance

Connection pooling reuses database connections instead of creating new ones per request. I use django-db-pool or configure persistent connections with CONN_MAX_AGE. This reduces connection overhead significantly. For high-traffic sites, I set appropri

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