Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_queryset method and ViewSets in django rest framework

Tags:

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?

like image 290
Yousuf Jawwad Avatar asked Jul 15 '13 09:07

Yousuf Jawwad


People also ask

What are ViewSets in Django REST framework?

After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Django REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet .

What is difference between Api_view and Viewset?

APIView allow us to define functions that match standard HTTP methods like GET, POST, PUT, PATCH, etc. Viewsets allow us to define functions that match to common API object actions like : LIST, CREATE, RETRIEVE, UPDATE, etc.

What is Queryset in Django REST framework?

The root QuerySet provided by the Manager describes all objects in the database table. Usually, though, you'll need to select only a subset of the complete set of objects. The default behavior of REST framework's generic list views is to return the entire queryset for a model manager.


1 Answers

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.

like image 97
Tom Christie Avatar answered Oct 06 '22 01:10

Tom Christie