Opened 4 years ago

Closed 3 years ago

#34339 closed New feature (wontfix)

Allow overriding construct_search().

Reported by: Ramez Issac Owned by: nobody
Component: contrib.admin Version: 4.1
Severity: Normal Keywords: admin search
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In ModelAdmin, get_search_result uses an inner function construct_search to add the icontains , istartswith, iexact

It can be much easier for more customization, if construct_search function was override-able.

FYI, My Case is: i can not add a related id field to search_fields... have to use traversing .

class MyAdmin(admin.ModelAdmin):
    search_fields = ['client_id', # wont work
                     'client__id'  # will work ]

Change History (4)

in reply to:  description comment:1 by Mariusz Felisiak, 4 years ago

Easy pickings: unset
Resolution: → wontfix
Status: new → closed
Summary: Enhancement: easier search field customization → Allow overriding construct_search().
Type: Uncategorized → New feature
class MyAdmin(admin.ModelAdmin):
    search_fields = ['client_id', # wont work
                     'client__id'  # will work ]

In such cases, you can always use the __exact lookup (as ​documented), e.g.

class MyAdmin(admin.ModelAdmin):
    search_fields = ['client_id__exact']

There is no need to change construct_search().

Last edited 4 years ago by Mariusz Felisiak (previous) (diff)

comment:2 by averta, 3 years ago

I would need to use search unaccent sensitive in the admin search.

Ideally, I would be able to override construct_search as :

def custom_construct_search(field_name):
    if field_name.startswith("^"):
        return "%s__unaccent__istartswith" % field_name[1:]
    elif field_name.startswith("="):
        return "%s__unaccent__iexact" % field_name[1:]
    elif field_name.startswith("@"):
        return "%s__unaccent__search" % field_name[1:]
    else:
        return "%s__unaccent__icontains" % field_name

How else would you recommend doing so ?

Last edited 3 years ago by averta (previous) (diff)

comment:3 by averta, 3 years ago

Resolution: wontfix
Status: closed → new

comment:4 by Natalia Bidart, 3 years ago

Resolution: → wontfix
Status: new → closed

Hello averta, please do not reopen tickets closed as wontfix without following the proper process (​see details).

If you need help understanding how Django works or how you can achieve something specifically, please see TicketClosingReasons/UseSupportChannels for ways to get help.

Note: See TracTickets for help on using tickets.
Back to Top