Repository navigation
Fix SearchFilter crash when search_fields uses the pk alias - #10071
Merged
Merged
Conversation
construct_search() maps 'pk' to the model's primary key field, but must_call_distinct() passed 'pk' straight to Options.get_field(), so a view with search_fields = ['pk'] (or a lookup through a relation such as 'author__pk') raised FieldDoesNotExist and returned a 500. Resolve the alias in must_call_distinct() the same way construct_search() does.
auvipy
self-requested a review
October 7, 2026 18:16
browniebroke
approved these changes
Oct 7, 2026
auvipy
approved these changes
Oct 8, 2026
SearchFilter crash when search_fields uses the pk alias
HosseinSayyedMousavi
pushed a commit
to HosseinSayyedMousavi/django-rest-framework
that referenced
this pull request
Oct 8, 2026
…0071) construct_search() maps 'pk' to the model's primary key field, but must_call_distinct() passed 'pk' straight to Options.get_field(), so a view with search_fields = ['pk'] (or a lookup through a relation such as 'author__pk') raised FieldDoesNotExist and returned a 500. Resolve the alias in must_call_distinct() the same way construct_search() does.
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.
Description
SearchFilter.construct_search()resolves thepkalias to the model's primary keyfield, so
search_fields = ['pk'](or'=pk', or a lookup through a relation such as'author__pk') builds a valid query. Butmust_call_distinct(), which runs right afterto decide whether duplicates are possible, passes
'pk'straight toOptions.get_field(), so the request fails withFieldDoesNotExistand a 500:This resolves the alias in
must_call_distinct()the same wayconstruct_search()does. Tests cover
pkon the model, through a foreign key, through a many-to-many(where distinct is still required, and a search by
pkthrough a view. All three failwithout the fix.