Skip to content

Avoid crawling the entire model graph when viewing RefreshTokenAdmin#1675

Merged
dopry merged 2 commits intodjango-oauth:masterfrom
IrishPrime:refresh-token-admin-select-related
Apr 1, 2026
Merged

Avoid crawling the entire model graph when viewing RefreshTokenAdmin#1675
dopry merged 2 commits intodjango-oauth:masterfrom
IrishPrime:refresh-token-admin-select-related

Conversation

@IrishPrime
Copy link
Copy Markdown
Contributor

Fixes #1674

Description of the Change

Explicitly set list_select_related to only look for related applications and users when viewing the RefreshTokenAdmin page. When this value is not set, highly connected model graphs (i.e. those where the user foreign-key is related to a large number of other models) can cause runaway recursion causing the admin page to fail to load.

Add a regression test that verifies the RefreshTokenAdmin changelist queryset uses bounded select_related. The test replicates the logic of ChangeList.apply_select_related and asserts that qs.query.select_related is a dict scoped to the declared fields rather than True (unbounded), which would cause Django to crawl the entire model graph.

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • CHANGELOG.md updated (only for user relevant changes)
  • author name in AUTHORS
  • tests/app/idp updated to demonstrate new features
  • tests/app/rp updated to demonstrate new features

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses Django admin changelist performance and stability for RefreshTokenAdmin by preventing Django from performing an unbounded select_related() traversal on highly connected model graphs (Fixes #1674).

Changes:

  • Set RefreshTokenAdmin.list_select_related to ("application", "user") to bound JOIN traversal.
  • Add a regression test asserting the changelist queryset’s select_related state is scoped (dict) rather than unbounded (True).
  • Document the fix in CHANGELOG.md and add the contributor to AUTHORS.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
oauth2_provider/admin.py Adds bounded list_select_related to RefreshTokenAdmin to avoid runaway JOIN recursion.
tests/test_settings.py Adds regression test verifying select_related is bounded for RefreshTokenAdmin queryset construction.
CHANGELOG.md Records the fix under “Fixed” in the unreleased section.
AUTHORS Adds the new contributor entry.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Add a regression test that verifies the `RefreshTokenAdmin` changelist
queryset uses bounded `select_related`. The test replicates the logic of
`ChangeList.apply_select_related` and asserts that `qs.query.select_related`
is a `dict` scoped to the declared fields rather than `True` (unbounded),
which would cause Django to crawl the entire model graph.
Explicitly set `list_select_related` to only look for related
applications and users when viewing the `RefreshTokenAdmin` page. When
this value is not set, highly connected model graphs (i.e. those where
the `user` foreign-key is related to a large number of other models) can
cause runaway recursion causing the admin page to fail to load.

Resolve django-oauth#1674.
@dopry dopry force-pushed the refresh-token-admin-select-related branch from ba6cc62 to 8fd54ef Compare April 1, 2026 02:59
Copy link
Copy Markdown
Member

@dopry dopry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IrishPrime excellent work! Thank you for finding this and submitting such a well formd and easy to review PR.

@dopry dopry merged commit b277f32 into django-oauth:master Apr 1, 2026
27 checks passed
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.

RefreshTokenAdmin changelist triggers unbounded select_related, causing Too many columns on MySQL

3 participants