ORM Lookups (Object-Relational Mapping lookups) are specialized query operators in Django's ORM system that allow developers to filter, compare, and retrieve database records using Python-like syntax rather than writing raw SQL.
Key Characteristics of ORM Lookups
-
Field-Specific Operations
-
Append to field names using double underscores (
__) -
Example:
price__gt,name__icontains
-
-
SQL Abstraction
-
Convert Python syntax to database queries automatically
-
Work across different database backends (PostgreSQL, MySQL, SQLite)
-
-
Chainable Filters
-
Can be combined for complex queries
-
Example:
Product.objects.filter(price__gt=100, stock__lt=10)
-
-
Type-Sensitive
-
Different lookups available for different field types:
-
Text fields:
contains,istartswith -
Numbers:
gt,lte -
Dates:
year,month
-
-
Why ORM Lookups Matter
-
Database Agnostic
-
Same code works with PostgreSQL, MySQL, etc.
-
-
Security
-
Automatic SQL injection protection
-
-
Readability
-
Expressive syntax closer to natural language than SQL
-
-
Performance
-
Lazy evaluation optimizes query execution
-