# 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
)
from django.db import models
from django.core.exceptions import ValidationError
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10, decimal_places=2)
specs = models.JSONField(default=dict, blank=True)
metadata = models.JSONField(default=dict, blank=True)
def clean(self):
"""Validate JSON structure."""
if self.specs:
required_keys = ['weight', 'dimensions']
if not all(k in self.specs for k in required_keys):
raise ValidationError(
f'specs must contain: {", ".join(required_keys)}'
)
class Meta:
indexes = [
models.Index(
fields=['specs'],
name='product_specs_idx'
)
]
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.