Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stefanofusai/drf-haystack-search-filter
Implement a django-haystack search filter in Django Rest Framework
https://github.com/stefanofusai/drf-haystack-search-filter
django-haystack django-rest-framewok
Last synced: 1 day ago
JSON representation
Implement a django-haystack search filter in Django Rest Framework
- Host: GitHub
- URL: https://github.com/stefanofusai/drf-haystack-search-filter
- Owner: stefanofusai
- License: mit
- Created: 2024-07-07T14:59:35.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-01-05T03:17:28.000Z (6 days ago)
- Last Synced: 2025-01-05T04:17:33.926Z (6 days ago)
- Topics: django-haystack, django-rest-framewok
- Language: Python
- Homepage:
- Size: 103 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# drf-haystack-search-filter
A [django-haystack](https://github.com/django-haystack/django-haystack) search filter for Django Rest Framework.
This package uses [uv](https://docs.astral.sh/uv/) for project management. To get started, make sure that **uv** is installed on your machine and updated to the latest version. Detailed installation instructions for **uv** can be found [here](https://docs.astral.sh/uv/getting-started/installation/).
## Installation
```bash
uv add drf-haystack-search-filter
```## Usage
Simply import the `HaystackSearchFilter` and use it in your API views:
```python
from drf_haystack_search_filter.filters import HaystackSearchFilter...
class MyAPIView(...):
...
filter_backends = [HaystackSearchFilter, ...]
...
```You can customize the search behavior by overriding the `_search` method.
```python
from typing import TypeVarfrom drf_haystack_search_filter import HaystackSearchFilter
T = TypeVar("T")
class MyHaystackSearchFilter(HaystackSearchFilter):
def _search(self, request: Request, queryset: QuerySet[T], view: APIView, query: str) -> QuerySet[T]:
# Customize the search behavior here
return queryset.filter(
pk__in=(
SearchQuerySet()
.models(queryset.model)
.filter(content__startswith=query)
.values_list("pk", flat=True)
)
)class MyAPIView(...):
...
filter_backends = [MyHaystackSearchFilter, ...]
...
```## Development
```bash
uv sync --frozen --group=development
uv run --frozen pre-commit install --install-hooks
uv run --frozen pre-commit install --hook-type=commit-msg
```## Contributing
Contributions are welcome! To get started, please refer to our [contribution guidelines](https://github.com/stefanofusai/drf-haystack-search-filter/blob/main/CONTRIBUTING.md).
## Issues
If you encounter any problems while using this package, please open a new issue [here](https://github.com/stefanofusai/drf-haystack-search-filter/issues).