python
# Find products with specific spec value
products = Product.objects.filter(specs__weight__gte=100)

# Check if JSON key exists
products = Product.objects.filter(specs__has_key='color')

Django JSON field for flexible schema data

django python jsonfield
by Priya Sharma 2 tabs
python
from django.db import models
from django.utils import timezone


class Event(models.Model):
    name = models.CharField(max_length=200)

Django timezone-aware datetime handling

django python timezone
by Priya Sharma 1 tab
python
import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent.parent

SECRET_KEY = os.environ.get('SECRET_KEY', 'dev-secret-key-change-in-production')

Django settings organization with environment-based configs

django python configuration
by Priya Sharma 3 tabs
python
from rest_framework.throttling import UserRateThrottle


class BurstRateThrottle(UserRateThrottle):
    """Allow short bursts of requests."""
    scope = 'burst'

Django REST Framework throttling for rate limiting

django python rest
by Priya Sharma 3 tabs
python
from django.db.models import Count, Avg, Sum, Q, F
from django.views.generic import TemplateView
from products.models import Product, Order, OrderItem


class DashboardView(TemplateView):

Django aggregation with annotate for statistics

django python database
by Priya Sharma 1 tab
python
from django.db import models
from django.utils import timezone
from datetime import date


class Person(models.Model):

Django model property for computed fields

django python models
by Priya Sharma 1 tab
python
import uuid
import logging


class RequestIDMiddleware:
    def __init__(self, get_response):

Django middleware for request ID tracking

django python middleware
by Priya Sharma 1 tab
python
import random


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

Django database routers for multiple databases

django python database
by Priya Sharma 1 tab
python
from django.conf import settings
from datetime import datetime


def site_settings(request):
    """Add site-wide settings to template context."""

Django context processors for global template variables

django python templates
by Priya Sharma 2 tabs
python
from django.contrib import admin
from django.utils.html import format_html
from .models import Post, Comment


class CommentInline(admin.TabularInline):

Django admin customization with ModelAdmin

django python admin
by Priya Sharma 1 tab
python
from django.db.models import Q
from django.views.generic import ListView
from .models import Product


class ProductSearchView(ListView):

Django Q objects for complex queries

django python queries
by Priya Sharma 1 tab
python
from django.core.exceptions import ValidationError


def validate_file_size(file):
    """Limit file size to 5MB."""
    max_size_mb = 5

Django file upload handling with validation

django python files
by Priya Sharma 2 tabs