yourlabs / django-autocomplete-light

A fresh approach to autocomplete implementations, specially for Django. Status: v4 alpha, v3 stable, v2 & v1 deprecated.
https://django-autocomplete-light.readthedocs.io
MIT License
1.79k stars 467 forks source link

Unable to load data based on value of other fields in form #1230

Open senorpinatta opened 3 years ago

senorpinatta commented 3 years ago

Expected Behavior Form option only displays values related to previously selected option in form.

Successfully tested using the linkedurls test package. The network listener showed the following message displaying a valid response_. successfulpost

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. displayerror.

I used two different approaches but got this error.

  1. 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. wholehtml

  2. 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. Inkedemptyresult_LI

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.

senorpinatta commented 3 years ago

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.