Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply filter to search results #25

Closed
igorpejic opened this issue Sep 4, 2015 · 5 comments
Closed

Apply filter to search results #25

igorpejic opened this issue Sep 4, 2015 · 5 comments

Comments

@igorpejic
Copy link

Is it possible to apply a filter on the results of the search specific for each request?
I would need something like this:

class ApplesView(ListModelMixin, HaystackGenericAPIView):
    index_models = [Apple]
    serializer_class = AppleSearchSerializer

    def get(self, request, *args, **kwargs):
        queryset=self.list(request, *args, **kwargs).filter(eater=request.user)
        return self.list(request, *args, **kwargs)

In this way I would display only the results which concern the creator of the request.

Thank you for the great project.

@rhblind
Copy link
Owner

rhblind commented Sep 6, 2015

Hi,
Absolutely! But keep in mind that the QuerySet is a SearchQuerySet not a regular one, so you need to have the field you want to filter on in the index.

So you are almost there in the example provided, but you should make sure that the eater field in the index is holding the user id (We can't index python models). Then make the filter like this:

queryset=self.list(request, *args, **kwargs).filter(eater=request.user.pk)

and you should be good to go!

@igorpejic
Copy link
Author

@rhblind Thank you for your help, but maybe I am doing something wrong.
I can't do what you suggested because

 self.list(request, *args, **kwargs)

is a rest framework response of type: <class 'rest_framework.response.Response'>, not a SearchQuerySet.

@rhblind
Copy link
Owner

rhblind commented Sep 6, 2015

Ah, right =)
In that case I'd suggest to sublcass from the HaystackViewSet where you can override ie. the filter_queryset() method.

Something like this should do it:

class AppleViewSet(HaystackGenericAPIView):

    # See http://drf-haystack.readthedocs.org/en/latest/basic_usage.html#views-py for how to set up the HaystackViewSet

    def filter_queryset(self):
        queryset = super(AppleViewSet, self).filter_queryset()
        return queryset.filter(eater=self.request.user.pk)

@igorpejic
Copy link
Author

Ok, thank you. I modified your code and the final solution would be this code:

class AppleViewSet(HaystackViewSet):

    # See http://drf-haystack.readthedocs.org/en/latest/basic_usage.html#views-py for how to set up the HaystackViewSet

    def filter_queryset(self, *args, **kwargs):
        queryset = super(AppleViewSet, self).filter_queryset(self.get_queryset())
        return queryset.filter(eater=self.request.user.pk)

@rhblind
Copy link
Owner

rhblind commented Sep 6, 2015

Yeah, sorry about the typo there.
That was what I meant :)

@rhblind rhblind closed this as completed Sep 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants