Skip to content

Commit

Permalink
feat: try django-tomselect
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Dec 17, 2024
1 parent f01e7be commit 5cf1ddf
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 6 deletions.
12 changes: 8 additions & 4 deletions apis_core/generic/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

from apis_core.generic.abc import GenericModel
from apis_core.generic.forms.fields import ModelImportChoiceField
from django_tomselect.forms import TomSelectModelChoiceField
from django_tomselect.widgets import TomSelectModelWidget, TomSelectIterablesWidget
from django_tomselect.app_settings import TomSelectConfig


class GenericImportForm(forms.Form):
Expand Down Expand Up @@ -70,8 +73,8 @@ def __init__(self, *args, **kwargs):
# override the fields pointing to other models,
# to make them use the autocomplete widgets
override_fieldtypes = {
"ModelMultipleChoiceField": autocomplete.ModelSelect2Multiple,
"ModelChoiceField": autocomplete.ModelSelect2,
"ModelMultipleChoiceField": TomSelectIterablesWidget,
"ModelChoiceField": TomSelectModelChoiceField,
"ModelImportChoiceField": autocomplete.ModelSelect2,
}
for field in self.fields:
Expand All @@ -81,9 +84,10 @@ def __init__(self, *args, **kwargs):
self.fields[field]._queryset.model
)
if issubclass(ct.model_class(), GenericModel):
url = reverse("apis_core:generic:autocomplete", args=[ct])
url = reverse("apis_core:generic:tautocomplete", args=[ct])
tsc = TomSelectConfig(url=url)
self.fields[field].widget = override_fieldtypes[clsname](
url, attrs={"data-html": True}
config = tsc
)
self.fields[field].widget.choices = self.fields[field].choices

Expand Down
1 change: 1 addition & 0 deletions apis_core/generic/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def to_url(self, value):
{"external_only": True},
name="autocompleteexternalonly",
),
path("tomselect-autocomplete", views.AutocompleteView.as_view(), name="tautocomplete"),
]
),
),
Expand Down
7 changes: 6 additions & 1 deletion apis_core/generic/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import namedtuple
from copy import copy

from django_tomselect.autocompletes import AutocompleteModelView
from dal import autocomplete
from django import forms, http
from django.conf import settings
Expand Down Expand Up @@ -60,10 +61,10 @@ class GenericModelMixin:
"""

def setup(self, *args, **kwargs):
super().setup(*args, **kwargs)
if contenttype := kwargs.get("contenttype"):
self.model = contenttype.model_class()
self.queryset = self.model.objects.all()
super().setup(*args, **kwargs)

def get_template_names(self):
template_names = []
Expand Down Expand Up @@ -507,3 +508,7 @@ def form_valid(self, form):

def get_success_url(self):
return self.object.get_absolute_url()


class AutocompleteView(GenericModelMixin, AutocompleteModelView):
permission_action_required = "view"
97 changes: 96 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ apis-override-select2js = ">=0.1,<0.3"
crispy-bootstrap4 = ">=2023.1,<2025.0"
django-simple-history = ">=3.6"
pydot = "^3.0.2"
django-tomselect = "^2024.12.2"

[tool.poetry.group.docs]
optional = true
Expand Down
3 changes: 3 additions & 0 deletions sample_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"apis_core.generic",
"apis_core.core",
"apis_core.documentation",
"django_tomselect",
]

MIDDLEWARE = [
Expand All @@ -72,6 +73,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"crum.CurrentRequestUserMiddleware",
"simple_history.middleware.HistoryRequestMiddleware",
"django_tomselect.middleware.TomSelectMiddleware",
]

# ROOT_URLCONF = "apis_core.urls"
Expand All @@ -88,6 +90,7 @@
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django_tomselect.context_processors.tomselect",
],
},
},
Expand Down

0 comments on commit 5cf1ddf

Please sign in to comment.