2👍
That’s not a problem with autocomplete_light, but with your Django query:
Patient.objects.filter(Q(name__icontains=q) | Q(surname__icontains = q))
That would select all Patient which have surname__icontains="John S"
or name__icontains="John S"
. That’s why you get no result. Check how django-cities-light does search: https://github.com/yourlabs/django-cities-light/blob/stable/3.x.x/cities_light/abstract_models.py
Or use django-haystack and implement a haystack backend, or redis…
Or, fallback to raw sql in choices_for_request
to filter against a concatenation of name and surname.
0👍
If a simple query (using .filter()
on a single field) or a complex one using Q
(see @jpic’s answer) is not fitting your needs, you might also make use of Django’s search features (for Postgres only), especially SearchVector
:
It will allow you to combine multiple fields to be searched against.
>>> from django.contrib.postgres.search import SearchVector
>>> Entry.objects.annotate(
... search=SearchVector('body_text', 'blog__tagline'),
... ).filter(search='Cheese')
[<Entry: Cheese on Toast recipes>, <Entry: Pizza Recipes>]
see https://docs.djangoproject.com/en/3.1/topics/db/search/#document-based-search and https://docs.djangoproject.com/en/3.1/ref/contrib/postgres/search/#django.contrib.postgres.search.SearchVector
- [Answered ]-Regex email validation django
- [Answered ]-Can not add bootstrap class with button using Django and Python
- [Answered ]-Templates Doesnot exist at /accounts/login
- [Answered ]-Django websocker error: AttributeError: 'super' object has no attribute 'init'
- [Answered ]-Django: Accessing OneToOneField in template