Searching for hashid's with the ModelAdmin's 'search_fields' feature?
See original GitHub issueIt would be very useful for our daily operations if we could search the hashed value in the standard ModelAdmin search widget.
I naively tried adding the field name to search_fields
but it doesn’t find the items I know are there. This was with an HashidAutoField with primary, salt and alphabet all set.
I’ve spend some downtime looking into it, and found ModelAdmin.get_search_results
in the docs, and I can make this search work by implementing it like this:
def get_search_results(self, request, queryset, search_term):
queryset, use_distinct = super().get_search_results(request, queryset, search_term)
queryset |= self.model.objects.filter(id=search_term)
return queryset, use_distinct
What is weird about it that my code doesn’t do any decoding; the regular queryset lookup on the field does it already.
So now I wonder if we can make it so we don’t need to cart this overridden method around but just use the field name in the search_fields
?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:13 (7 by maintainers)
Top Results From Across the Web
Hashids - generate short unique ids from integers
Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers. It converts numbers like 347 into strings like “yr8”,...
Read more >How to add Search_fields in Django - python - Stack Overflow
I tried to add a search_fields for title and body by using Following code. class BlogAdmin(admin.ModelAdmin): . . . search_fields = ('title',' ...
Read more >ElateMe: Backend II.
The goal of this Bachelor thesis is to expand the ElateMe API service so that it can be used in a production environment....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This should now work with version 1.3.0 without any extra code in your ModelAdmin classes.
Gotcha, not at the moment, sorry.