Skip to main content

A simple package to implement a django-haystack search filter in Django Rest Framework

Project description

drf-haystack-search-filter

A simple package to implement a django-haystack search filter in Django Rest Framework.

Installation

pip install drf-haystack-search-filter

Usage

Simply import the HaystackSearchFilter and use it in your API views:

from drf_haystack_search_filter.filters import HaystackSearchFilter

...

class MyAPIView(...):
    ...
    filter_backends = [HaystackSearchFilter, ...]
    ...

You can customize the search behavior by overriding the _search method.

from typing import TypeVar

from drf_haystack_search_filter.filters 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, ...]
    ...

Contributing

Contributions are welcome! To get started, please refer to our contribution guidelines.

Issues

If you encounter any problems while using this package, please open a new issue here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_haystack_search_filter-1.0.0.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page