Filter advisor/mentor autocomplete dropdowns by role (#1126)#1393
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 viaPositionInline.formfield_for_foreignkey. Butco_advisorandgrad_mentorareautocomplete_fields: their options come from the admin autocomplete JSON endpoint (AutocompleteJsonView→ the target model admin'sget_search_results), which bypassesformfield_for_foreignkeyentirely. So those two search boxes were never filtered.Fix
Add
PersonAdmin.get_search_resultsthat reads the requesting Position field (model_name/field_namepassed by the autocomplete view) and narrows results to the same role-appropriate querysets used for the plain dropdowns:advisor/co_advisor→get_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.pyhit 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
formfield_for_foreignkey).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