DEV Community

Gahyun Son
Gahyun Son

Posted on

1

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay