[Fixed]-Limit the queryset of autocomplete fields in Django


This question look hold but I’ve seen many people keep asking for it and other give very complicated answers..

Then the simplest way to limit the autocomplete list is to limit the queryset by surcharging the get_search_results
or the get_queryset inside the related object’s ModelAdmin

Now to know when its called by your autocomplete field you have:

  • request.path : that will always contain ‘autocomplete’ string
  • request.GET.get(‘model_name’) : model name, on your exemple its the
    model ‘box’
  • request.GET.get(‘field_name’) : field name, on your exemple its the
    field ‘testkit’

Then your code should look like that:

class TestkitAdmin(admin.ModelAdmin):
    search_fields = ['number']

    def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct = super().get_search_results(request, queryset, search_term)
        if 'autocomplete' in request.path and request.GET.get('model_name') == 'box' and request.GET.get('field_name') == 'testkit':
            queryset = queryset.exclude(testkit__in=Box.objects.all().values('testkit'))
        return queryset, use_distinct


You can override the Django autocomplete.js static file in static/admin/js/autocomplete.js. Django will always prefer the file you have overwritten.

Then in the overwritten file modify the djangoAdminSelect2() function something like this:

$.fn.djangoAdminSelect2 = function(options) {
    var settings = $.extend({}, options);
    $.each(this, function(i, element) {
        var $element = $(element);

        $.extend(settings, {
            'ac_field_name': $element.parents().filter('.related-widget-wrapper').find('select').attr('name')

        init($element, settings);
    return this;

and the init() function like this:

var init = function($element, options) {
    var settings = $.extend({
        ajax: {
            data: function(params) {
                return {
                    term: params.term,
                    page: params.page,
                    field_name: options.ac_field_name
    }, options);

Then in your get_search_results() just access the request GET parameter field_name and voila.

Of course you can make the JS look nicer, but in principle it works like this.

Leave a comment