Django 6.1.3 on SQLite (in-memory): ch07-querysets Managers, QuerySet._clone(), and PreventQuerySetCloning type(Story.objects)=ManagerFromStoryQuerySet _queryset_class=StoryQuerySet built_with_as_manager=True chained 3 methods -> queries=0 distinct_qs=3 distinct_query=3 clone shares _hints=False shares _known_related_objects=True inside _avoid_cloning(): same_qs_object=True cloning_restored=True Evaluation, _result_cache, count/exists, and the `if qs:` slicing trap uncached qs[:2] -> queries=0 type=StoryQuerySet result_cache_is_none=True uncached qs.exists() -> queries=1 result=True sql=SELECT %s AS "a" FROM "newsroom_story" LIMIT 1 uncached qs.count() -> queries=1 result=6 sql=SELECT COUNT(*) AS "__count" FROM "newsroom_story" bool(qs) [if qs:] -> queries=1 cached_rows=6 has_LIMIT=False after bool(qs): qs[:2] type=list len=2 queries=0 (count=6, exists=True also 0 SQL!) Iterables: ModelIterable, ValuesIterable, ValuesListIterable, and iterator() all() -> iterable=ModelIterable item_type=Story values('id', 'slug') -> iterable=ValuesIterable item_type=dict values_list('id', 'slug') -> iterable=ValuesListIterable item_type=tuple values_list('slug', flat=True) -> iterable=FlatValuesListIterable item_type=str iterator(chunk_size=3) -> yielded=6 _result_cache=None prefetch_related().iterator() without chunk_size -> ValueError: chunk_size must be provided when using QuerySet.iterator() after prefetch_related(). select_related() SQL JOIN and RelatedPopulator column slicing select_related('desk', 'reporter') JOIN count=2 total_cols=19 column spans: Story[0:10] + Desk[10:14], Reporter[14:19] iterate 6 stories reading .desk.name + .reporter.name -> queries=1 sample=('Metro', 'Ada') prefetch_related(), Prefetch(to_attr=...), and cache invalidation on .filter() prefetch_related('corrections', 'beats') -> queries=3 Q1: SELECT "newsroom_story"."id", "newsroom_story"."headline", "newsroom_story"."slug", "newsroom_stor Q2: SELECT "newsroom_correction"."id", "newsroom_correction"."story_id", "newsroom_correction"."note", Q3: SELECT ("newsroom_storybeat"."story_id") AS "_prefetch_related_val_story_id", "newsroom_beat"."id" story._prefetched_objects_cache keys=['beats', 'corrections'] story.corrections.all() + beats.all() -> queries=0 (served from _prefetched_objects_cache) story.corrections.filter(...) -> queries=1 (cloning drops prefetch cache!) Prefetch(..., to_attr='ordered_corrections') -> queries=2 attr_type=list Django 6.1 fetch modes: FETCH_ONE vs FETCH_PEERS vs FETCH_RAISE FETCH_ONE (6 stories -> .desk + .reporter) -> queries=13 (1 + 12 N+1 queries) FETCH_PEERS (6 stories -> .desk + .reporter) -> queries=3 peers_tracked=6 peer batch SQL: SELECT "newsroom_desk"."id", "newsroom_desk"."name", "newsroom_desk"."slug", "newsroom_desk"."section_code" FROM "newsroom_desk" WHERE ("newsroom_desk"."id") IN ((%s), (%s), (%s)) FETCH_PEERS + .defer('body') across 6 stories -> queries=2 (1 initial + 1 batch IN) FETCH_RAISE unjoined .desk -> queries=0 FieldFetchBlocked: Fetching of Story.desk blocked. Bulk writes: update(), bulk_create(), bulk_update(), and _result_cache qs.update(priority=1) -> rows=1 queries=1 cache_before=True cache_after=None bulk_create(3) -> queries=1 all_pks_populated=True has_RETURNING=True bulk_update(3, ['note']) -> rows=3 has_CASE_WHEN=True bulk_update SQL: UPDATE "newsroom_correction" SET "note" = CASE WHEN ("newsroom_correction"."id" = %s) THEN %s WHEN ("newsroom_correction"."id" = %s) THEN %s WHEN ("newsroom_correction"."id" = %s) THEN %s ELSE NULL END WHERE "newsroom_correction"."id" IN (%s, %s, %s)