Opened 9 days ago

Last modified 8 days ago

#29010 new New feature

Allow customizing the autocomplete search results based on the querying model

Reported by: Muslu Y. Owned by: nobody
Component: contrib.admin Version: 2.0
Severity: Normal Keywords: ForeignKey, get_search_results, search_fields
Cc: Johannes Hoppe Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have A,B,C models. A and B models are using C model's ForeignKey for autocomplete.

How can i find which model is querying?

Change History (3)

comment:1 Changed 9 days ago by Muslu Y.

Summary: Which models is using search_fields to get_search_resultsWhich models is querying search_fields to autocomplete?

comment:3 Changed 9 days ago by Muslu Y.

I think Autocomplete is not enough right now.

models.py

class A(models.Model):
    a_name = models.CharField(max_length=100)

class B(models.Model):
    b_name   =   models.ManyToManyField(A)
    
class C(models.Model):
    c_name   =   models.ManyToManyField(A)

admin.py

class AAdmin(admin.ModelAdmin):
    search_fields       =   ['a_name']

    def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct  =   super().get_search_results(request, queryset, search_term)
        
        ## how can i learn which models (B or C) querying for a_name?
        ## because i want to separately filter for every model


class BAdmin(admin.ModelAdmin):
    autocomplete_fields    =   ['b_name']
    
class CAdmin(admin.ModelAdmin):
    autocomplete_fields    =   ['c_name']

comment:4 Changed 9 days ago by Tim Graham

Cc: Johannes Hoppe added
Component: Database layer (models, ORM)contrib.admin
Summary: Which models is querying search_fields to autocomplete?Allow customizing the autocomplete search results based on the querying model

I see. I guess it might be possible to add a hint of the model where the query is coming from in a GET parameter. That would be untrusted though.

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