Skip to content

Commit

Permalink
fix(generic): use dal widget only for models with autocomplete endpoint
Browse files Browse the repository at this point in the history
The generic form overrides the default select widgets with autocomplete
widgets provided by the django autocomplete light app. This can only
work for contenttypes that provide an autocomplete endpoint. We can only
be sure of that for models that inherit from the GenericModel, so we
skip the override for all the others.

Closes: #764
  • Loading branch information
b1rger committed Apr 12, 2024
1 parent c4fe0e3 commit 1de88f0
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions apis_core/generic/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from crispy_forms.layout import Submit
from dal import autocomplete
from apis_core.generic.forms.fields import ModelImportChoiceField
from apis_core.generic.abc import GenericModel


class GenericImportForm(forms.Form):
Expand Down Expand Up @@ -85,8 +86,9 @@ def __init__(self, *args, **kwargs):
ct = ContentType.objects.get_for_model(
self.fields[field]._queryset.model
)
url = reverse("apis_core:generic:autocomplete", args=[ct])
self.fields[field].widget = override_fieldtypes[clsname](
url, attrs={"data-html": True}
)
self.fields[field].widget.choices = self.fields[field].choices
if issubclass(ct.model_class(), GenericModel):
url = reverse("apis_core:generic:autocomplete", args=[ct])
self.fields[field].widget = override_fieldtypes[clsname](
url, attrs={"data-html": True}
)
self.fields[field].widget.choices = self.fields[field].choices

0 comments on commit 1de88f0

Please sign in to comment.