From 6b63c54b9fddbc4b9168646e12692711ea0b66ee Mon Sep 17 00:00:00 2001 From: marcelo-maldonado95 Date: Sat, 6 Dec 2025 15:46:48 -0600 Subject: [PATCH] Fix jQuery preference order to prevent conflicts with third-party widgets When other Django admin widgets (like PrettyJSONWidget, JSONEditor, etc.) load their own jQuery, they overwrite the global `jQuery` variable. Since django_select2.js prefers `jQuery` over `window.django.jQuery`, it ends up using the newly loaded jQuery instance which doesn't have Select2 attached. This causes "$element.select2 is not a function" errors on admin pages. By preferring `window.django.jQuery` (Django's protected instance), we ensure Select2 functionality works regardless of what other widgets do to the global jQuery variable. --- django_select2/static/django_select2/django_select2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_select2/static/django_select2/django_select2.js b/django_select2/static/django_select2/django_select2.js index f299e367..766c3bec 100644 --- a/django_select2/static/django_select2/django_select2.js +++ b/django_select2/static/django_select2/django_select2.js @@ -5,8 +5,8 @@ } else if (typeof module === 'object' && module.exports) { module.exports = factory(require('jquery')) } else { - // Browser globals - factory(jQuery || window.django.jQuery) + // Browser globals - prefer Django's jQuery to avoid conflicts + factory(window.django.jQuery || jQuery) } }(function ($) { 'use strict'