Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Gahyun Son
Gahyun Son

Posted on

     

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

I tried this unittest.

deftest_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 modeExit fullscreen mode

and I got an error.

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

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

classPhotoDetailView(generics.ListAPIView):serializer_class=PhotoDetailSerializerdefget_queryset(self):queryset=Photos.objects.get(id=self.kwargs['photo_id'])returnqueryset
Enter fullscreen modeExit 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.

classPhotoDetailView(generics.ListAPIView):serializer_class=PhotoDetailSerializerdefget_queryset(self):queryset=Photos.objects.filter(id=self.kwargs['photo_id'])returnqueryset
Enter fullscreen modeExit fullscreen mode

Wow, it works! I'm happy

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hello, I'm a Software developer.
  • Education
    Korea open national university
  • Joined

More fromGahyun Son

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp