Skip to content

Conversation

@marcelo-maldonado95
Copy link

Problem

When using django-select2 with Django admin (Grappelli or standard), other widgets
that load their own jQuery (like PrettyJSONWidget, JSONEditor, django-summernote, etc.)
can overwrite the global jQuery variable.

Since django_select2.js currently prefers jQuery over window.django.jQuery: ```javascript factory(jQuery || window.django.jQuery)

It uses the newly loaded jQuery instance which doesn't have Select2 attached, causing: $element.select2 is not a function

Solution

Reverse the preference order to use Django's protected jQuery instance first: factory(window.django.jQuery || jQuery)

This ensures Select2 functionality works regardless of what other widgets do to the global jQuery variable.

Related

This complements Fix #292 which ensures proper JS media loading order. That fix controls django-select2's own loading order, but can't prevent third-party widgets from loading jQuery afterward.

…gets

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

i18n is broken due to missing jQuery import in media 🐛

1 participant