Reputation: 25
I am using Django REST Framework to build my own API. I've built multiple methods without any problems but I stumbled upon a situation where I can't find a solution.
I have a GET method which internally creates a queryset which gets multiple instances from the same model that are related with the current user. This is how my view looks like:
class getTheList(generics.ListAPIView):
serializer_class = GetTheThingListSerializer
def get_queryset(self):
user = self.request.user
the_thing = TheThing.objects.get(pk=self.kwargs['pk'])
return the_thing.get_other_things(user)
As it can be seen, I have overriden get_queryset()
. Now according to the documentation, I should pass the queryset and many=True
as parameters for the serializer. However, I have no way to access this custom queryset (self.get_queryset() won't work obviously). And if I do not pass the many=True
parameter, the serializer will only receive one object and not multiple.
I have been successful with querysets when defined at the top of the class, but not when get_queryset()
is overriden. How should I proceed?
Upvotes: 0
Views: 1361
Reputation: 29977
The base class ListAPIView
internally instantiates the serializer with many=true
, as you can see in the source.
If your get_queryset()
method actually return a queryset, you should be fine.
Only when you instantiate the serializer directly, then you have to pass many=true
Upvotes: 1