#32628: Add extra data to autocomplete request ------------------------------------------------+------------------------ Reporter: Seb G | Owner: nobody Type: Cleanup/optimization | Status: new Component: contrib.admin | Version: 3.2 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | ------------------------------------------------+------------------------ More often than not, I need to add extra parameters to requests sent by autocomplete widgets from admin forms. Currently I have 2 options to do so:
1. Listen to opening/closing Select2 events to add headers to the request: {{{#!javascript $(document).on("select2:opening", "#foo_set-group select.admin- autocomplete", function () { $.ajaxSetup({headers: {"X-Foo": "bar"}}); } ).on("select2:closing", "#foo_set-group select.admin-autocomplete", function () { delete $.ajaxSettings.headers["X-Foo"]; } ); }}} 2. Destroy the widget and rebuild it using `$.fn.djangoAdminSelect2` to hack my extra parameters next to the other expected GET parameters: {{{#!python let $select = $("#id_my_field"); $select.select2("destroy").djangoAdminSelect2({ ajax: { data: function (params) { return { term: params.term page: params.page app_label: $select.data("app-label"), model_name: $select.data("model-name"), field_name: $select.data("field-name") foo: "bar" } } } }); }}} None of these solutions look clean to me, and I would definitely like to have the ability to add extra parameters to `$.fn.djangoAdminSelect2` properly as follows: - `autocomplete.js`: {{{#!javascript 'use strict'; { const $ = django.jQuery; const init = function($element, options, extraParams) { const settings = $.extend({ ajax: { data: function(params) { let defaultParams = { term: params.term, page: params.page, app_label: $element.data('app-label'), model_name: $element.data('model-name'), field_name: $element.data('field-name') }; return $.extend(defaultParams, extraParams) } } }, options); $element.select2(settings); }; $.fn.djangoAdminSelect2 = function(options, extraParams) { const settings = $.extend({}, options); $.each(this, function(i, element) { const $element = $(element); init($element, settings, extraParams); }); return this; }; $(function() { // Initialize all autocomplete widgets except the one in the template // form used when a new formset is added. $('.admin- autocomplete').not('[name*=__prefix__]').djangoAdminSelect2(); }); $(document).on('formset:added', (function() { return function(event, $newFormset) { return $newFormset.find('.admin- autocomplete').djangoAdminSelect2(); }; })(this)); } }}} - `consumer_script.js`: {{{#!javascript $select = $("#id_my_field"); $select.select2("destroy").djangoAdminSelect2({}, {foo: "bar"}); }}} -- Ticket URL: <https://code.djangoproject.com/ticket/32628> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/049.66924cb4e5bace1c07c91b1bc154dbf3%40djangoproject.com.