[Django]-Getting Multiple Objects using Django rest_framework RetrieveAPIView

4👍

You can’t use RetrieveAPIView for several instances.
However, you likely want some filtering. I’d recommend using url arguments for that along with the ListAPIView:

http://www.django-rest-framework.org/api-guide/filtering/#filtering-against-the-url

1👍

From the docs of RetrieveAPIView

Used for read-only endpoints to represent a single model instance.

RetrieveAPIView is used to serialize and return exactly one object, which is returned by either get_object(self) method or by a lookup on queryset.

If you want to display/return multiple filtered objects, you can use ListAPIView. Your filtering can be done in the get_queryset(self) method, something as following

def get_queryset(self):
    return Shop.Objects.filter(Suburb = self.suburb)

Or better, use drf’s built-in Filtering.

Leave a comment