From 1de88f05d74f95b93e476057f31cddfcb1cd05a2 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Thu, 11 Apr 2024 10:52:29 +0200 Subject: [PATCH] fix(generic): use dal widget only for models with autocomplete endpoint 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 --- apis_core/generic/forms/__init__.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/apis_core/generic/forms/__init__.py b/apis_core/generic/forms/__init__.py index 39e66e6d3..a3c6f9457 100644 --- a/apis_core/generic/forms/__init__.py +++ b/apis_core/generic/forms/__init__.py @@ -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): @@ -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