Django ORM 5.2
Last reviewed Sep 24, 2026
Prompt Details
Prompt Composition
3prompts
- Python 3155 lines · 3.8K tokens
- Django 5.2157 lines · 4.1K tokens
- Django ORM 5.2107 lines · 4.1K tokens
Total419 lines · 12K tokens
CWE Coverage
- 20
- 23
- 36
- 59
- 73
- 78
- 79
- 89
- 95
- 117
- 201
- 203
- 208
- 209
- 214
- 250
- 287
- 295
- 307
- 319
- 328
- 338
- 347
- 352
- 362
- 367
- 379
- 384
- 409
- 416
- 426
- 427
- 434
- 470
- 502
- 521
- 524
- 532
- 601
- 611
- 613
- 614
- 639
- 640
- 674
- 732
- 770
- 772
- 776
- 778
- 798
- 807
- 829
- 862
- 863
- 915
- 916
- 918
- 942
- 1004
- 1021
- 1104
- 1275
- 1333
- 1336
- 1395
Prompt Preview
Apply the Django 5.2 prompt and its Python baseline first. Use these ORM-specific rules for Django 5.2.x, including the security fixes through 5.2.17, on supported Python 3.10–3.14 releases; Python 3.14 support starts with Django 5.2.8.
Query construction and SQL escape hatches
- Build lookup dictionaries from a fixed mapping of public filter names to permitted fields, relations, and lookups. Never unpack request dictionaries directly into
filter(),exclude(),get(), orQ(), or expose internal arguments such as_connectorand_negated. - Keep annotation and aggregate aliases,
FilteredRelationnames, andorder_by()expressions server-selected; patched alias validation does not authorize access to arbitrary fields or related models. - Put tenant and policy predicates into the parameterized SQL itself when using
QuerySet.raw(); it executes a new query and ignores earlier queryset filters. - Treat
.raw()as capable of writes: Django does not enforce a statement type. Use read-only database privileges when a raw-query path must be read-only. - Validate and typecast MySQL parameters in raw queries and custom
get_prep_value()implementations; numeric comparisons against string columns can match unintended rows through coercion. - Put a limit in raw SQL when bounding returned rows; slicing a
RawQuerySetdoes not add a database limit. - Keep
QuerySet.explain()options under operator control.analyze=Truecan execute the query and invoke functions with side effects even for aSELECT.
Tenant scope and related data
- Preserve scope inside