简体   繁体   中英

Exact field search in the Django admin

I'm trying to configure the Django admin bindings for a specific model so that I can quickly filter by the exact value of a specific field. I know I can manually tamper with the GET parameters to do this:

?fieldname__exact=foobar

What I can't seem to do, is get the sidebar to display a small form where I can enter this value. I know I can add fieldname to list_filter , but that doesn't scale well beyond tens of unique values. I looked into django-advanced-filters for a bit, but it doesn't seem to be compatible with Django 1.11.

How can I implement exact search for a specific field in the Django admin?

a little late but i hope it still helps someone

the possible configurations for the searchfield are listed at http://www.django-rest-framework.org/api-guide/filtering/#searchfilter .

 '^' Starts-with search. '=' Exact matches. (case-insensitive) '@' Full-text search. (Currently only supported Django's MySQL backend.) '$' Regex search. (Only with Django Rest Framework) 

In your case, just add

search_fields = ['=fieldname', 'otherfieldname']

to your admin.

Update: Since Django 2.1 it is also possible to add lookups to the search fields (eg 'fieldname__exact'). You can find infos in the Django Docs , as well as the possible Lookups

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM