Django ORM 5.2

Last reviewed Sep 24, 2026

Export as PDF

Prompt Details

Prompt Composition

3prompts
  1. Python 3155 lines · 3.8K tokens
  2. Django 5.2157 lines · 4.1K tokens
  3. 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(), or Q(), or expose internal arguments such as _connector and _negated.
  • Keep annotation and aggregate aliases, FilteredRelation names, and order_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 RawQuerySet does not add a database limit.
  • Keep QuerySet.explain() options under operator control. analyze=True can execute the query and invoke functions with side effects even for a SELECT.

Tenant scope and related data

  • Preserve scope inside