Skip to content

Pass the paginator its count instead of patching the queryset - #1665

Merged
danlamanna merged 1 commit into
masterfrom
paginate-count-argument
Nov 24, 2026
Merged

danlamanna merged 1 commit into
masterfrom
paginate-count-argument

Conversation

@danlamanna

@danlamanna danlamanna commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

qs_with_hardcoded_count replaced a queryset's count method with a lambda, which any clone, such as the paginator's own order_by, silently dropped, and the lesion list set a custom_count attribute with the same problem. paginate and paginate_queryset take the total as a count argument instead.

  • A given count is returned on every page, as the lesion list already did; queryset.count() still only runs on the first page.
  • Callers: the image list and search (Elasticsearch count), the image browser, the collection detail page (materialized view count) and the lesion list.
  • No performance change on its own, since the same queries run: benchmarked alone against master on the 100k dataset, every paginated endpoint is within 5%.

Independent of the permissions change that removes the distinct() calls these endpoints still carry.

Summary by CodeRabbit

  • Bug Fixes
    • Image and collection listings now display more consistent total counts across pages, including when counts come from cached or search results.
    • Image ordering now follows the configured sorting rules consistently, including pinned-image sorting.

qs_with_hardcoded_count replaced a queryset's count method with a
lambda, which any clone, such as the paginator's own order_by, silently
dropped, and the lesion list set a custom_count attribute with the same
problem. paginate and paginate_queryset take the total as a count
argument instead. A given count is returned on every page, as the lesion
list already did, while queryset.count() still only runs on the first.
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Pagination now accepts a separate optional count. When no count is supplied, it counts the queryset only on the first cursor page. Image APIs, image browser, collection detail, and lesion list pass counts through the pagination interface.

Changes: Pagination Count Propagation

Layer / File(s) Summary
Pagination count handling
isic/core/pagination.py
paginate and CursorPagination.paginate_queryset accept an optional count. Without one, the paginator counts the queryset only when the cursor has no position.
Image API count handling
isic/core/api/image.py
Image list and search pass Elasticsearch counts to pagination when enabled. Image pagination always applies request-based ordering.
View count sources
isic/core/views/images.py, isic/core/views/collections.py, isic/ingest/api.py
The image browser passes its computed total, collection detail passes a cached count when available, and lesion list passes its user-specific count. The image browser renders the computed total.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Refactor

Suggested reviewers: brianhelba, annehaley

Merge Risk: Minimal · up to 95b6f

This change passes the total count to pagination explicitly instead of attaching it to the queryset, and no merge-blocking risk was found in the supplied changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Pass the paginator its count instead of patching the queryset" matches the main change. The PR adds an optional count argument to paginate and CursorPagination.paginate_queryset, and remove…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Pass the paginator its count instead of patching the queryset" matches the main change. The PR adds an optional count argument to paginate and CursorPagination.paginate_queryset, and removes the custom_count and qs_with_hardcoded_count approach that patched the queryset. The title is concise, specific, and states the primary change from the developer's perspective. The changed files (pagination.py, image.py, collections.py, images.py, ingest/api.py) all relate to this change.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 867460b0-fce6-4dbe-8ddf-4d1fb6f394ab
Commits

Reviewing files that changed from the base of the PR and between cb1faa9 and 95b6fef.

Files selected for processing: 5

  • isic/core/api/image.py
  • isic/core/pagination.py
  • isic/core/views/collections.py
  • isic/core/views/images.py
  • isic/ingest/api.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@danlamanna
danlamanna merged commit 0b3eefa into master Nov 24, 2026
3 checks passed
@danlamanna
danlamanna deleted the paginate-count-argument branch November 24, 2026 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant