question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Searching for hashid's with the ModelAdmin's 'search_fields' feature?

See original GitHub issue

It 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
nshafercommented, Sep 25, 2017

This should now work with version 1.3.0 without any extra code in your ModelAdmin classes.

0reactions
nshafercommented, Jul 15, 2019

Gotcha, not at the moment, sorry.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found