Make search unaccented
This commit is contained in:
parent
71c5de49d3
commit
0a5559b4df
|
@ -16,6 +16,7 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
|
'django.contrib.postgres',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'easy_thumbnails',
|
'easy_thumbnails',
|
||||||
'recette',
|
'recette',
|
||||||
|
|
|
@ -2,6 +2,8 @@ from django import forms
|
||||||
|
|
||||||
from .models import Recette
|
from .models import Recette
|
||||||
|
|
||||||
|
STOP_WORDS = ['et', 'de', 'des', 'a', 'aux']
|
||||||
|
|
||||||
|
|
||||||
class SearchForm(forms.Form):
|
class SearchForm(forms.Form):
|
||||||
text = forms.CharField(
|
text = forms.CharField(
|
||||||
|
@ -17,7 +19,9 @@ class SearchForm(forms.Form):
|
||||||
if self.cleaned_data['veget']:
|
if self.cleaned_data['veget']:
|
||||||
qs = qs.filter(is_veget=True)
|
qs = qs.filter(is_veget=True)
|
||||||
if self.cleaned_data['text']:
|
if self.cleaned_data['text']:
|
||||||
qs = qs.filter(nom__icontains=self.cleaned_data['text'])
|
terms = [t for t in self.cleaned_data['text'].split() if t not in STOP_WORDS]
|
||||||
|
for term in terms:
|
||||||
|
qs = qs.filter(nom__unaccent__icontains=term)
|
||||||
if self.cleaned_data['saison'] != 'all':
|
if self.cleaned_data['saison'] != 'all':
|
||||||
qs = qs.filter(saison__contains=[self.cleaned_data['saison']])
|
qs = qs.filter(saison__contains=[self.cleaned_data['saison']])
|
||||||
return qs
|
return qs
|
||||||
|
|
11
recette/migrations/0005_postgres_unaccent.py
Normal file
11
recette/migrations/0005_postgres_unaccent.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
from django.contrib.postgres.operations import UnaccentExtension
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('recette', '0004_recette_photo_instr'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [UnaccentExtension()]
|
Loading…
Reference in a new issue