Opened 22 months ago

Closed 9 months 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, 22 months ago

Easy pickings: unset
Resolution: wontfix
Status: newclosed
Summary: Enhancement: easier search field customizationAllow overriding construct_search().
Type: UncategorizedNew 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, e.g.

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

There is no need to change construct_search().

Version 0, edited 22 months ago by Mariusz Felisiak (next)

comment:2 by averta, 9 months 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 9 months ago by averta (previous) (diff)

comment:3 by averta, 9 months ago

Resolution: wontfix
Status: closednew

comment:4 by Natalia Bidart, 9 months ago

Resolution: wontfix
Status: newclosed

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