我在我的项目中使用django-autocomplete-light插件。插件工作正常,但如果公司名称由多个单词组成,则它不会通过第二个或第三个单词进行搜索,例如Bayerische Motoren Werke(宝马)如果我搜索Baye ...它会找到它,但是搜索Mo ...或者Wer ...它不会。我知道它是一个自动完成插件,但我想知道是否有解决方法。
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,)
答案 0 :(得分:2)
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
尝试将istartswith
替换为icontains