[Solved]-Integrating django-haystack with django-rest-framework?

14👍

After a lot of trial and error, i have found the right combination! Here is a start.

Define a serializer: serializers.py

class DotaSearchSerializer(serializers.Serializer):
    text = serializers.CharField()
    name = serializers.CharField()
    quality = serializers.CharField()
    type = serializers.CharField()
    rarity = serializers.CharField()
    hero = serializers.CharField()
    image = serializers.CharField()
    desc = serializers.CharField()

Create the view: views.py

class DotaSearchViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):

    serializer_class = DotaSearchSerializer
    permission_classes = (IsAuthenticated,)
    authentication_classes = (SessionAuthentication, BasicAuthentication)

    def get_queryset(self, *args, **kwargs):
        request = self.request
        queryset = EmptySearchQuerySet()

        if request.GET.get('q') is not None:
            query = request.GET.get('q')
            queryset = SearchQuerySet().filter(content=query)

        return queryset

Please note you might want to clean the input and perform other security checks.

Route it: urls.py

router.register(r'search', api_views.DotaSearchViewSet, base_name='search')
👤Deep

Leave a comment