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.
Metadata
Release files for django-search-vector-model-mixin 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_search_vector_model_mixin-0.1.2.tar.gz | 3.4 kB | Without provenance | Details for django_search_vector_model_mixin-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_search_vector_model_mixin-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for django_search_vector_model_mixin-0.1.2-py3-none-any.whl |
Total release size: 7.8 kB
Release files / django_search_vector_model_mixin-0.1.2.tar.gz
| Download URL | django_search_vector_model_mixin-0.1.2.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5dda631e5a3b5bb443edfff7806cb5a5e078decaf94bee2fb606f303bcedd401
|
|
BLAKE2b-256 checksum How to use checksums |
6afe9837b0a0a99415fb5e868bff58129888add047f57f33e80e881a57505783
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
|
Release files / django_search_vector_model_mixin-0.1.2-py3-none-any.whl
| Download URL | django_search_vector_model_mixin-0.1.2-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
98fe63d3a77afe355568b032cbc87bc74af158dfc709c4c5bc595f191814e0b4
|
|
BLAKE2b-256 checksum How to use checksums |
d7a52799cf37e9a35880b075d8575741d6009b9c96ad3fe0cf6f494b33a68d9a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
|
Security for django-search-vector-model-mixin 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.