I'm using django-autocomplete-light with django 1.8. It sometimes fails. Here is the javascript console error:
TypeError: $(...).select2 is not a function
;(function ($) {
$(document).on('autocompleteLightInitialize', '[data-autocomplete-light-function=select2]', function() {
var element = $(this);
// This widget has a clear button
$(this).find('option[value=""]').remove();
$(this).select2({
It worked fine yesterday and nothing has changed but today it is not working. I'm also using django suit. I feel that there is a javascript incompatibility between my django apps causing this random behavious but I do not know how to track down the cause. Any ideas?
Here is all of my code:
# models.py
class Sample(models.Model):
name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
class Doctor(models.Model):
name = models.CharField(max_length=200)
address = models.CharField(max_length=200, null=True, blank=True, default='')
def __unicode__(self):
return self.name
class Patient(models.Model):
name = models.CharField(max_length=200)
sample = models.OneToOneField(Sample, null=True, blank=True)
doctor = models.ForeignKey(Doctor, null=True, blank=True, default=None)
def __unicode__(self):
return self.name
----------
# urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^pat-sam-autocomplete/$', PatientSampleAutocomplete.as_view(), name='pat-sam-autocomplete',),
url(r'^pat-doc-autocomplete/$', PatientDoctorAutocomplete.as_view(), name='pat-doc-autocomplete',),
)
----------
# views.py
class PatientSampleAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated():
return Sample.objects.none()
qs = Sample.objects.all()
if self.q:
qs = qs.filter(name__icontains=self.q)
return qs
class PatientDoctorAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated():
return Doctor.objects.none()
qs = Doctor.objects.all()
if self.q:
qs = qs.filter(name__icontains=self.q)
return qs
--------------
# admin.py
from django import forms
from dal import autocomplete
class PatientForm(forms.ModelForm):
class Meta:
model = Patient
fields = ('__all__')
widgets = {
'sample': autocomplete.ModelSelect2(url='pat-sam-autocomplete'),
'doctor': autocomplete.ModelSelect2(url='pat-doc-autocomplete')
}
@admin.register(Doctor)
class DoctorAdmin(admin.ModelAdmin):
list_display = ('name', 'address')
@admin.register(Sample)
class SampleAdmin(admin.ModelAdmin):
list_display = ('name',)
@admin.register(Patient)
class PatientAdmin(admin.ModelAdmin):
list_display = ('name', 'sample')
form = PatientForm