DEV Community

Gahyun Son
Gahyun Son

Posted on

Django API | queryset & object, filter() & get()

I tried this unittest.

    def test_price_of_photo_list(self):
        photo1 = create_photos()
        create_prices(photo1)

        url = detail_url(photo1.id)
        res = self.client.get(url)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
Enter fullscreen mode Exit fullscreen mode

and I got an error.

TypeError: 'Photos' object is not iterable
Enter fullscreen mode Exit fullscreen mode

First, Let's go to the views.py.

class PhotoDetailView(generics.ListAPIView):
    serializer_class = PhotoDetailSerializer

    def get_queryset(self):
        queryset = Photos.objects.get(id=self.kwargs['photo_id'])
        return queryset
Enter fullscreen mode Exit fullscreen mode

I wrote a Photo object. It could be cause.

  • .get(): returns a object.
  • .filter(): returns a queryset.

OK. We can't return a object to run. Then why?

ListAPIView expects a queryset. (not a object)
In documents,

ListAPIView
Used for read-only endpoints to represent a collection of model instances.
Provides a get method handler.
Extends: GenericAPIView, ListModelMixin

Move to GenericAPIView and ListModelMixin.

GenericAPIView
This class extends REST framework's APIView class, adding commonly required behavior for standard list and detail views.
Each of the concrete generic views provided is built by combining GenericAPIView, with one or more mixin classes.
Attributes
Basic settings:
The following attributes control the basic view behavior.
queryset - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the get_queryset() method. If you are overriding a view method, it is important that you call get_queryset() instead of accessing this property directly, as queryset will get evaluated once, and those results will be cached for all subsequent requests.

ListModelMixin
Provides a .list(request, *args, **kwargs) method, that implements listing a queryset.
If the queryset is populated, this returns a 200 OK response, with a serialized representation of the queryset as the body of the response. The response data may optionally be paginated.

As you can see two api expect queryset. Then what I can modify?

  • .filter(): returns a queryset.

Let's change the method to filter.

class PhotoDetailView(generics.ListAPIView):
    serializer_class = PhotoDetailSerializer

    def get_queryset(self):
        queryset = Photos.objects.filter(id=self.kwargs['photo_id'])
        return queryset
Enter fullscreen mode Exit fullscreen mode

Wow, it works! I'm happy

Top comments (0)