Giter Site home page Giter Site logo

Comments (5)

igorpejic avatar igorpejic commented on June 27, 2024 2

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)

from drf-haystack.

rhblind avatar rhblind commented on June 27, 2024 1

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!

from drf-haystack.

igorpejic avatar igorpejic commented on June 27, 2024

@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.

from drf-haystack.

rhblind avatar rhblind commented on June 27, 2024

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)

from drf-haystack.

rhblind avatar rhblind commented on June 27, 2024

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

from drf-haystack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.