23

I am doing exactly as the example states

here is my method

class FeedViewSet(viewsets.ModelViewSet):
    model = Feed
    serializer_class = FullFeedSerializer

    def get_queryset(self):
        user = request.user
        queryset = Feed.objects.get_nearby(user)
        return queryset

when i execute it, it says request not defined .. which actually isn't. the example at the rest framework's site also haven't defined request. what am i doing wrong?

1 Answer 1

33

The request object is available (on either REST framework's class based views, or Django's standard class based views) as self.request. You're missing the self. part of that.

Sign up to request clarification or add additional context in comments.

4 Comments

hey tom, i figured it out, but the documentation misses the self. also :)
django-rest-framework.org/api-guide/viewsets.html .. under the heading ModelViewSet , the code example has this mistake.
how to access request object in serializers or all the business logic should be done in views only ?
"how to access request object in serializers" - If you're using the generic views, then it'll be available on a serializer as self.context['request']. If you're not using the generic views then you'll need to make sure to pass context={'request': request} when instantiating the serializer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.