python - django-autocomplete-light using SQL Alchemy instead of Django ORM -
i use django-autocomplete-light autocomplete fields external database cannot adjusted conform djangoorm requirements. therefore use sql alchemy connect db.
i cannot find out how this. example make autocomplete use following instead of django model table (which doesn't work because there dual column primary key , no id field.
query = (session.query(tablea.firstname).distinct(tablea.firstname) .filter(tablea.firstname.match(name))) data = query.limit(100).all()
effectively make field autocomplete names above query. instead of using django qs shown in documentation:
class countryautocomplete(autocomplete.select2querysetview): def get_queryset(self): # don't forget filter out results depending on visitor ! if not self.request.user.is_authenticated(): return country.objects.none() qs = country.objects.all() if self.q: qs = qs.filter(name__istartswith=self.q) return qs class personform(forms.modelform): birth_country = forms.modelchoicefield( queryset=country.objects.all(), widget=autocomplete.modelselect2(url='country-autocomplete') )
ok, think managed find solution.
i can use select2listview acting upon sql alchemy query returns 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 have 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 dbc.extract_distinct_typecode call function uses sql alchemy extract list of codes. have limited length of list of codes speed good.
and in urls.py have following:
urlpatterns = [ url(r'^typecode-autocomplete/$', select2listviewautocomplete.as_view(), name='typecode-autocomplete'),]
its idea ensure user authenticated url doesn't return results user.
Your blog is in a convincing manner, thanks for sharing such an information with lots of your effort and time
ReplyDeletesql server dba online training
sql database administrator training