-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added django-filter plugin - add multiselect search
- Loading branch information
1 parent
8973156
commit 914981b
Showing
8 changed files
with
375 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import django_filters | ||
from django import forms | ||
from django.contrib.postgres.search import SearchHeadline, SearchQuery, SearchRank | ||
from django.db.models import F | ||
from django_filters import ModelMultipleChoiceFilter, MultipleChoiceFilter | ||
|
||
from general.models import DocumentFile, Institution, Language, Subject | ||
|
||
|
||
class DocumentFileFilter(django_filters.FilterSet): | ||
search = django_filters.CharFilter(method="filter_search", label="Search") | ||
institution = ModelMultipleChoiceFilter( | ||
queryset=Institution.objects.all(), widget=forms.CheckboxSelectMultiple | ||
) | ||
document_type = MultipleChoiceFilter( | ||
choices=DocumentFile.document_type_choices, widget=forms.CheckboxSelectMultiple | ||
) | ||
subjects = ModelMultipleChoiceFilter( | ||
queryset=Subject.objects.all(), widget=forms.CheckboxSelectMultiple | ||
) | ||
languages = ModelMultipleChoiceFilter( | ||
queryset=Language.objects.all(), widget=forms.CheckboxSelectMultiple | ||
) | ||
|
||
class Meta: | ||
model = DocumentFile | ||
fields = [ | ||
"document_type", | ||
"institution", | ||
"subjects", | ||
"languages", | ||
] | ||
|
||
def filter_search(self, queryset, name, value): | ||
if value: | ||
queue = SearchQuery(value) | ||
search_rank = SearchRank(F("search_vector"), queue) | ||
|
||
queryset = ( | ||
queryset.annotate( | ||
rank=search_rank, | ||
) | ||
.defer("document_data") | ||
.select_related("institution") | ||
.filter(search_vector=queue) | ||
.order_by("-rank") | ||
) | ||
return queryset | ||
|
||
else: | ||
return "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# forms.py | ||
import django_filters | ||
from django import forms | ||
|
||
from general.models import DocumentFile | ||
|
||
|
||
class DocumentFileSearchForm(forms.Form): | ||
title = django_filters.CharFilter(lookup_expr="iexact") | ||
# title = django_filters.CharFilter(lookup_expr='iexact') | ||
|
||
class Meta: | ||
model = DocumentFile | ||
fields = ["subjects", "languages"] | ||
|
||
|
||
# class DocumentFileSearchForm(forms.Form): | ||
# query = forms.CharField(label='Search', max_length=255, required=False) | ||
# | ||
# subjects = forms.MultipleChoiceField( | ||
# choices=[], | ||
# required=False, | ||
# widget=forms.SelectMultiple | ||
# ) | ||
# | ||
# languages = forms.MultipleChoiceField( | ||
# choices=[], | ||
# required=False, | ||
# # widget=forms.CheckboxSelectMultiple | ||
# widget=forms.SelectMultiple | ||
# ) | ||
# | ||
# def __init__(self, *args, **kwargs): | ||
# super().__init__(*args, **kwargs) | ||
# | ||
# self.fields['subjects'].choices = [(cat, cat) for cat in | ||
# Subject.objects.values_list('name', flat=True).distinct()] | ||
# self.fields['languages'].choices = [(brand, brand) for brand in | ||
# Language.objects.values_list('name', flat=True).distinct()] |
Oops, something went wrong.