django-autocomplete-light
django-autocomplete-light copied to clipboard
Unable to load data based on value of other fields in form
Expected Behavior Form option only displays values related to previously selected option in form.
Successfully tested using the linked_urls test package. The network listener showed the following message displaying a valid response.
Actual Behaviour
The following error message is displayed when trying to link the list of available options to a previously selected value in the form.
.
I used two different approaches but got this error.
-
Using autocomplete.ModelSelect2(url='sites/site/add/', forward=...) and the following is what the network listener recorded. Response field seems to send the whole page's html.
-
Using autocomplete.ModelSelect2(url='site-list', forward=...) and the following is what the network listener recorded. "site-list" was chosen for being one of the possible urls show from
print(router.get_urls())
. Response field displays null values.
Relevant code snippets:
views.py
class SiteViewSet(FileExportNamingMixin, DynamicFieldsViewMixin, viewsets.ReadOnlyModelViewSet, autocomplete.Select2QuerySetView):
queryset = Site.objects.all()
serializer_class = SiteSerializer
def get_queryset(self):
qs = super(SiteViewSet, self).get_queryset()
cn = self.forwarded.get('country', None)
if cn:
qs = qs.filter(country=cn)
return qs
form.py
class LocaleForm(forms.ModelForm):
class Meta:
model = Site
fields = ('country', 'subnationalRegion')
widgets = {
'subnationalRegion': autocomplete.ModelSelect2(url='site-list', forward=('country',))
#'subnationalRegion': autocomplete.ModelSelect2(url='sites/site/add', forward=('country',))
}
urls.py
from rest_framework import routers
from .views import SiteViewSet
router = routers.DefaultRouter()
router.register(r'sites', SiteViewSet)
admin.py
@admin.register(Site)
class TestAdmin(admin.ModelAdmin):
form = LocaleForm
Between the two errors (1,2 using different url addresses for the autocomplete.ModelSelect2 function) I'm hoping someone can point out my implementation error or verify if this is a bug. The demo package seems to have configured the urls using django's url_patterns, whereas my preexisting project uses rest_framework's router library, maybe the conflict lies there.
Further review leads me to believe that approach 1 is incorrect.
Further inspection also shows the difference between the urls in the responses.
To operate successfully, the response url would be: "../admin/sites/site/add/.." or something along those lines.
The issue is that it is using "../api/paginated/sites.../" instead. So this is probably less an DACL issue, and more of my own django/url configuration issue.. I'll post the solution if/when I have one.