Model Mixin for Full Test Search in PostgreSQL with support of multilanguage sites
Project description
Django Search Vector Model Mixin
This Django app provides a mixin for adding a search vector field to a model.
App supports Multilingual Search Vector fields, which depend on the settings.LANGUAGES.
This functionality is useful for a combination with django-modeltranslation, because django-modeltranslation provides a model field for each language and doesn't support multilingual SearchVectorField.
This app is focused on PostgreSQL and GinIndex.
Installation
With PIP:
pip install django-search-vector-model-mixin
With Poetry:
poetry add django-search-vector-model-mixin
Usage
Add app to INSTALLED_APPS
INSTALLED_APPS = [
...
'django_search-vector-model-mixin'
]
Add mixin to your model
Example with single search_vector field
The following example shows how to add a search vector field to a model.
from django_search_vector_model_mixin.models import SearchVectorModelMixin
class MyModel(SearchVectorModelMixin, models.Model):
search_fields = ['title', 'description'] # write fields, which will be added to search vector
title = models.CharField(max_length=255)
description = models.TextField()
# inherit from SearchVectorModelMixin.Meta for support of GinIndex
class Meta(SearchVectorModelMixin.Meta):
pass
In this case, the search vector field will be named 'search_vector'.
Example with multilingual search_vector field
The following example shows how to add a search vector field to a model with multilingual support.
from django_search_vector_model_mixin.models import MultilingualSearchVectorModelMixin
class MyModel(MultilingualSearchVectorModelMixin, models.Model):
search_fields = ['title', 'description'] # write fields, which will be added to search vector
title = models.CharField(max_length=255)
description = models.TextField()
# inherit from SearchVectorModelMixin.Meta for support of GinIndex
class Meta(MultilingualSearchVectorModelMixin.Meta):
pass
MultilingualSearchVectorModelMixin works with django-modeltranslation.
It means you should configure translator for selected search_fields by django-modeltranslation documentation.
For example:
translator.py
from modeltranslation.translator import translator, TranslationOptions
from .models import MyModel
class MyModelTranslationOptions(TranslationOptions):
fields = ('title', 'description')
translator.register(MyModel, MyModelTranslationOptions)
How to use search vector field
SearchView
from django.db.models import Q
from django.utils.translation import get_language
from django.views.generic import ListView
class SearchView(ListView):
template_name = 'search/search_objects.html'
paginate_by = 10
multi_language = False # set True if you use multilingual search_vector field
with_tags = True
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({'search': self.request.GET.get('search', None) or ''})
return context
def get_queryset(self):
qs = super().get_queryset()
query = self.request.GET.get('search', None)
if query:
if self.multi_language:
current_language = get_language()
return qs.filter(
Q(**{'search_vector_{}'.format(current_language): query}) |
Q(search_vector=query)
)
return qs.filter(search_vector=query)
return qs
urls.py
from django.urls import path
from .views import SearchView
from .models import MyModel
app_name = 'search'
urlpatterns = [
path('mymodels/', views.SearchView.as_view(queryset=MyModel.objects.all(), multi_language=True), name='mymodels'),
]
Conclusion
This app is a solution, which is used by projects, developed by Evgenii Legotckoi
This solution is provided for community "AS IS" without a warranty and can be used for any purpose under MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_search_vector_model_mixin-0.1.2.tar.gz.
File metadata
- Download URL: django_search_vector_model_mixin-0.1.2.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5dda631e5a3b5bb443edfff7806cb5a5e078decaf94bee2fb606f303bcedd401
|
|
| MD5 |
3d82e51169fb369679c39eada4a2c265
|
|
| BLAKE2b-256 |
6afe9837b0a0a99415fb5e868bff58129888add047f57f33e80e881a57505783
|
File details
Details for the file django_search_vector_model_mixin-0.1.2-py3-none-any.whl.
File metadata
- Download URL: django_search_vector_model_mixin-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98fe63d3a77afe355568b032cbc87bc74af158dfc709c4c5bc595f191814e0b4
|
|
| MD5 |
74c125dac1b8993124fa69340403c78e
|
|
| BLAKE2b-256 |
d7a52799cf37e9a35880b075d8575741d6009b9c96ad3fe0cf6f494b33a68d9a
|