2
I can suggest to use the PostgreSQL Full-Text Search with Django.
The official documentations is quite good.
If you want more information and motivation about my suggestion you can read an article I wrote on this subject: Full-Text Search in Django with PostgreSQL
1
FYI the SearchVector/SearchQuery approach actually does not catch all cases, for example partial words (see https://www.fusionbox.com/blog/detail/partial-word-search-with-postgres-full-text-search-in-django/632/ for reference). You can implement your own without much trouble, depending on your constraints.
example, within a viewsets’ get_queryset method:
...other params...
search_terms = self.request.GET.get('q')
if search_terms:
# remove possible other delimiters and other chars
# that could interfere
cleaned_terms = re.sub(r'[!\'()|&;,]', ' ', search_terms).strip()
if cleaned_terms:
# Check against all the params we want
# apply to previous terms' filtered results
q = reduce(
lambda p, n: p & n,
map(
lambda word:
Q(your_property__icontains=word) | Q(
second_property__icontains=word) | Q(
third_property__icontains=word)
cleaned_terms.split()
)
)
qs = YourModel.objects.filter(q)
return qs
- [Django]-Changing django-storages backend from from s3 to cloudfiles and dealing with old files
- [Django]-Django – Optimal way to sort models by boolean operation
- [Django]-Django-Tagging – count and ordering top "tags" (Is there a cleaner solution to mine?)
- [Django]-How do I call templates with same names at different directories in Django?
- [Django]-Django: prevent template from using a model method
Source:stackexchange.com