I am using django-autocomplete-light plugin in my project. Plugin works just fine, but if the company name is made from more than one word it doesn't search by the second or third word, eg Bayerische Motoren Werke(BMW) if I search for Baye... it fill find it, but searching Mo... or Wer... it won't. I know it's an autocomplete plugin, but I am wondering if there is a workaround.
views.py
class CompanyAutoComplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Company.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q) | qs.filter(comNumber__istartswith=self.q)
return qs
urls.py
urlpatterns += [
url(
r'^autocomplete/$',
views.CompanyAutoComplete.as_view(model=Company),
name='company-autocomplete',
),
]
models.py
class Company(models.Model):
name = models.CharField(max_length=255)
comNumber = models.CharField(max_length=255)
law = models.CharField(max_length=255)
country = models.CharField(max_length=255, null=True, blank=True)
city = models.CharField(max_length=255, null=True, blank=True)
street = models.CharField(max_length=255, null=True, blank=True)
house_number = models.CharField(max_length=255, null=True, blank=True)
email = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.name.encode("utf-8")
def get_absolute_url(self):
return reverse('company-detail', args=[str(self.id)])
def __unicode__(self):
return '%s' % (self.name,)
class CompanyAutoComplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Company.objects.all()
if self.q:
qs = qs.filter(name__icontains=self.q) | qs.filter(comNumber__icontains=self.q)
return qs
Try to replace istartswith
with icontains
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.