python 41 lines · 2 tabs

Django JSON field for flexible schema data

Priya Sharma Jan 2026
2 tabs
# 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')

# Contains lookup
products = Product.objects.filter(
    specs__contains={'material': 'aluminum'}
)

# PostgreSQL-specific: path lookups
products = Product.objects.filter(
    specs__dimensions__width__gte=50
)
2 files · python Explain with highlit

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 operators. I validate JSON structure in forms or model clean methods. Unlike pickled data, JSON is readable and can be queried. For frequently-queried JSON fields, I add database indexes on specific keys. This balances flexibility with queryability. For complex relational data, I still prefer proper foreign keys and tables.


Related snips

python
import os
import stat

for root, _dirs, files in os.walk('/etc'):
    for name in files:
        path = os.path.join(root, name)

Python security audit script for exposed risky filesystem state

python auditing host-security
by Kai Nakamura 1 tab
python
class Product(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    cost = models.DecimalField(max_digits=10, decimal_places=2)
    margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)

Django model signals vs overriding save

django python models
by Priya Sharma 2 tabs
python
from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [

Django URL namespacing and reverse lookups

django python urls
by Priya Sharma 3 tabs
ruby
# Vulnerable: user input is concatenated directly into SQL.
email = params[:email]
password = params[:password]

sql = "SELECT * FROM users WHERE email = '#{email}' AND password_hash = '#{password}'"
user = ActiveRecord::Base.connection.execute(sql).first

SQL injection prevention with unsafe and safe query patterns

sql-injection owasp database
by Kai Nakamura 3 tabs
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment


class PostType(DjangoObjectType):

Django GraphQL with Graphene

django python graphql
by Priya Sharma 2 tabs
sql
-- EXPLAIN ANALYZE (actual execution statistics)
EXPLAIN ANALYZE
SELECT u.username, COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.created_at >= '2024-01-01'

Advanced query optimization techniques

database optimization query-performance
by Maria Garcia 2 tabs

Share this code

Here's the card — post it anywhere.

Django JSON field for flexible schema data — share card
Link copied