django

python
from products.models import Product


def import_products_bulk(product_data):
    """Import thousands of products efficiently."""
    products = [

Django bulk operations for performance

django python performance
by Priya Sharma 1 tab
python
from rest_framework import serializers
from .models import Author, Book


class BookSerializer(serializers.ModelSerializer):
    class Meta:

Django REST Framework nested serializers with writable fields

django python rest
by Priya Sharma 1 tab
python
from django.db import models


class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()

Django model inheritance with proxy models

django python models
by Priya Sharma 1 tab
python
import uuid
from django.db import models


class Cart(models.Model):
    OPEN = "open"

Prevent Duplicate Order Submissions in Django with select_for_update Row Locking

django postgres concurrency
by codesnips 3 tabs
erb
{% load cache %}
<section class="dashboard">
  <h1>{{ team.name }} — Overview</h1>

  {% cache 3600 team_order_stats team.id stats_version %}
    <div class="stat-grid">

Cache an Expensive Dashboard Fragment with Django's Template Cache Tag and Signal-Based Invalidation

django caching template-cache
by codesnips 3 tabs
python
from django.db import migrations


def populate_slugs(apps, schema_editor):
    """Generate slugs for existing posts."""
    Post = apps.get_model('blog', 'Post')

Django database migrations best practices

django python migrations
by Priya Sharma 1 tab
python
import random


class ReadWriteRouter:
    """Route reads to replicas, writes to primary."""

Django database read-write split with routers

django python database
by Priya Sharma 3 tabs
python
from blog.models import Post


# Load only specific fields
posts = Post.objects.only('id', 'title', 'published_at')
for post in posts:

Django query optimization with only and defer

django python database
by Priya Sharma 1 tab
python
import magic
from django import forms
from django.core.exceptions import ValidationError
from django.template.defaultfilters import filesizeformat

Validating Upload Size and MIME Type in a Custom Django Form Field

django forms file-upload
by codesnips 3 tabs
python
import threading
from contextlib import contextmanager

_state = threading.local()

Per-Tenant Data Isolation in Django with a Thread-Local Current Tenant Manager

django multi-tenancy orm
by codesnips 4 tabs
python
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',

Django database connection pooling for performance

django python database
by Priya Sharma 1 tab
python
# Debug Toolbar setup
INSTALLED_APPS += ['debug_toolbar']

MIDDLEWARE = [
    'debug_toolbar.middleware.DebugToolbarMiddleware',
] + MIDDLEWARE

Django debug toolbar for development

django python debugging
by Priya Sharma 1 tab