Skip to content

Filter advisor/mentor autocomplete dropdowns by role (#1126)#1393

Merged
jonfroehlich merged 1 commit into
masterfrom
1126-advisor-mentor-autocomplete-filter
Jun 24, 2026
Merged

Filter advisor/mentor autocomplete dropdowns by role (#1126)#1393
jonfroehlich merged 1 commit into
masterfrom
1126-advisor-mentor-autocomplete-filter

Conversation

@jonfroehlich

Copy link
Copy Markdown
Member

Closes #1126.

Problem

When an admin edits a Person and sets an advisor / co-advisor / mentor on an inline Position, the dropdowns offered every lab member regardless of role — e.g. undergrads as advisors (the original screenshots), a PhD student as co-advisor, an undergrad as mentor. There was save-time validation, but only after the editor had already picked an invalid option ("it shouldn't let me get that far").

Root cause

The plain advisor <select> was already role-filtered via PositionInline.formfield_for_foreignkey. But co_advisor and grad_mentor are autocomplete_fields: their options come from the admin autocomplete JSON endpoint (AutocompleteJsonView → the target model admin's get_search_results), which bypasses formfield_for_foreignkey entirely. So those two search boxes were never filtered.

Fix

Add PersonAdmin.get_search_results that reads the requesting Position field (model_name/field_name passed by the autocomplete view) and narrows results to the same role-appropriate querysets used for the plain dropdowns:

  • advisor / co_advisorget_active_professors_queryset()
  • grad_mentor ("Mentor") → get_active_mentors_queryset() (senior members; professors are advisors, not mentors)

Tests

New integration tests in website/tests/test_advisor_mentor_autocomplete.py hit the real autocomplete endpoint and pin the role filtering for both fields. They fail before this change and pass after. All 33 related admin/person tests stay green.

Resolves all three parts of #1126

  1. Advisor dropdown role-filtering — was already fixed (formfield_for_foreignkey).
  2. co_advisor / mentor autocomplete role-filtering — fixed here.
  3. Auto-focus the search box on open (follow-up comment) — now works out of the box via the Select2 bundled with the current Django; no change needed.

Screenshots

This is an admin-internal behavior change (no public-facing UI). Behavior is pinned by the new endpoint tests rather than screenshots — happy to add a before/after of the filtered autocomplete if preferred.

🤖 Generated with Claude Code

The advisor field's plain <select> was already role-filtered via
PositionInline.formfield_for_foreignkey, but co_advisor and grad_mentor
are autocomplete_fields: their options come from the admin autocomplete
JSON endpoint (AutocompleteJsonView -> PersonAdmin.get_search_results),
which bypasses formfield_for_foreignkey. So the autocomplete search
offered every person -- e.g. undergrads as co-advisors, exactly the
bug in #1126. Save-time validation caught bad picks, but only after
the editor had already selected an invalid option.

Add PersonAdmin.get_search_results that reads the requesting Position
field (model_name/field_name passed by the autocomplete view) and
narrows results to the same role-appropriate querysets used for the
plain dropdowns: active professors for advisor/co_advisor, active
senior members for grad_mentor.

Add integration tests that hit the real autocomplete endpoint and pin
the role filtering for both fields (failing before this change).

The remaining ask on #1126 (auto-focus the search box on open) is now
handled for free by the Select2 bundled with the current Django.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jonfroehlich jonfroehlich merged commit 6189f43 into master Jun 24, 2026
3 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.

Admins editing advisors/mentors are shown all lab member as options

1 participant