Meepo Meepo - 6 months ago 10
Python Question

Return X number of objects from Cursa.objects.filter

I've been looking for a way to return specific number of object in the json response when I use the 'GET' method.

class CursaEsport(generics.ListAPIView):
serializer_class = CursaSerializer

def get_queryset(self):

sport = self.kwargs['sport']
return Cursa.objects.filter(esport=sport, visible=True)


If I use this it will return ALL the objects that accomplish the conditions of the filter but I would like to get only 50 objects. I'm not sure that is posible.

Answer

Since Cursa.objects.filter returns a list, simply limited the number of objects to at most 50 by:

Cursa.objects.filter[:50]

This is known as splicing