Opened 4 months ago

Last modified 3 months 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: Someday/Maybe
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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 (5)

comment:1 Changed 4 months 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 4 months ago by Muslu Y.

I think Autocomplete is not enough right now.

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)

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 4 months 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.

comment:5 Changed 3 months ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:6 Changed 3 months ago by Johannes Hoppe

Triage Stage: AcceptedSomeday/Maybe

Hi there,

currently you don't explicitly know who is calling this method, since it's all called from the same view. Both Widgets call the same view as the view is on AAdmin.
So this is conceptually not supported. That doesn't mean it doesn't work. You can always check

To add a bit more context here, we did have an intermediate solution that would have made this easier. A view per widget. We ultimately dropped that approach to decrease complexity. A decision I still support.

My suggestion would be use an external library like django-select2 or django-autocomplete if you want to implement more sophisticated logic.
Please also keep in mind, the admin is not recommend to be used for sofistikated user interfaces.

Anyhow, at the moment I would strongly advice against adding a "hint" to the request. Since it's not safe against request forging and can lead to unintended security issues.

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