[Answered ]-Django-autocomplete-light using SQL Alchemy instead of Django ORM

2👍

✅

Ok, I think I managed to find my solution.

I can use the Select2ListView acting upon the SQL Alchemy query which returns a list:

in views.py

from .forms import get_choice_list

class Select2ListViewAutocomplete(autocomplete.Select2ListView):
    def create(self, text):
        return text

    def get_list(self):
        return get_choice_list(name=self.q)

In forms.py I have the following:

from dal import autocomplete

def get_choice_list(name=''):
    return dbc.extract_distinct_typecode(typeCode=name)

class SelectTypeForm(forms.Form):
    typeCode = autocomplete.Select2ListCreateChoiceField(choice_list=get_choice_list, widget=autocomplete.ListSelect2(url='typecode-autocomplete'))

where the dbc.extract_distinct_typecode is the call to a function that uses SQL Alchemy to extract a list of codes. I have limited the length of the list of codes so that the speed is good.

and in urls.py I have the following:

urlpatterns = [
    url(r'^typecode-autocomplete/$', Select2ListViewAutocomplete.as_view(), name='typecode-autocomplete'),]

Its probably a good idea to ensure the user is authenticated so that the url doesn’t return the results to any user.

Leave a comment