django-autocomplete-light icon indicating copy to clipboard operation
django-autocomplete-light copied to clipboard

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

Open senorpinatta opened this issue 3 years ago • 1 comments

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. 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 avatar Mar 29 '21 04:03 senorpinatta

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.

senorpinatta avatar Mar 29 '21 10:03 senorpinatta