Skip to content

Add Select2 mixin that uses Django's own select template #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion django_select2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"""
from django import get_version

if get_version() < '3.2':
if get_version() < "3.2":
default_app_config = "django_select2.apps.Select2AppConfig"
29 changes: 24 additions & 5 deletions django_select2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

import django
from django import forms
from django.contrib.admin.widgets import SELECT2_TRANSLATIONS
from django.contrib.admin.widgets import SELECT2_TRANSLATIONS, AutocompleteMixin
from django.core import signing
from django.db.models import Q
from django.forms.models import ModelChoiceIterator
Expand All @@ -65,7 +65,9 @@
from .conf import settings

if django.VERSION < (4, 0):
from django.contrib.admin.utils import lookup_needs_distinct as lookup_spawns_duplicates
from django.contrib.admin.utils import (
lookup_needs_distinct as lookup_spawns_duplicates,
)
else:
from django.contrib.admin.utils import lookup_spawns_duplicates

Expand All @@ -79,6 +81,9 @@ class Select2Mixin:
form media.
"""

css_class_name = "django-select2"
theme = None

empty_label = ""

def __init__(self, *args, **kwargs):
Expand All @@ -90,7 +95,7 @@ def build_attrs(self, base_attrs, extra_attrs=None):
default_attrs = {
"lang": self.i18n_name,
"data-minimum-input-length": 0,
"data-theme": settings.SELECT2_THEME,
"data-theme": self.theme or settings.SELECT2_THEME,
}
if self.is_required:
default_attrs["data-allow-clear"] = "false"
Expand All @@ -102,9 +107,9 @@ def build_attrs(self, base_attrs, extra_attrs=None):
attrs = super().build_attrs(default_attrs, extra_attrs=extra_attrs)

if "class" in attrs:
attrs["class"] += " django-select2"
attrs["class"] += " " + self.css_class_name
else:
attrs["class"] = "django-select2"
attrs["class"] = self.css_class_name
return attrs

def optgroups(self, name, value, attrs=None):
Expand Down Expand Up @@ -137,6 +142,20 @@ def media(self):
)


class Select2AdminMixin:
"""Select2 mixin that uses Django's own select template."""

css_class_name = "admin-autocomplete"
theme = "admin-autocomplete"

@property
def media(self):
return forms.Media(
js=Select2Mixin().media._js,
css=AutocompleteMixin(None, None).media._css,
)


class Select2TagMixin:
"""Mixin to add select2 tag functionality."""

Expand Down
18 changes: 18 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ModelSelect2TagWidget,
ModelSelect2Widget,
Select2Widget,
Select2AdminMixin,
)
from tests.testapp import forms
from tests.testapp.forms import (
Expand Down Expand Up @@ -175,6 +176,23 @@ def test_theme_setting(self, settings):
assert 'data-theme="classic"' in widget.render("name", None)


class TestSelect2AdminMixin:
def test_media(self):
translation.activate("en")
assert tuple(Select2AdminMixin().media._js) == (
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js",
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/en.js",
"django_select2/django_select2.js",
)

assert dict(Select2AdminMixin().media._css) == {
"screen": [
"admin/css/vendor/select2/select2.min.css",
"admin/css/autocomplete.css",
]
}


class TestSelect2MixinSettings:
def test_default_media(self):
sut = Select2Widget()
Expand Down