I am in Python 2.7 and Django 1.7.1 with django-restframework I have an API that returns me some specific values taken fron the Database, it uses a Custom Serializer like this:
class InventarioSerializer(serializers.ModelSerializer):
item = serializers.RelatedField(source='producto.item')
ubicacion = serializers.RelatedField(source='ubicacion.nombre')
class Meta:
model = Inventario
fields = ('epc','item','cantidad','ubicacion')
My API's view is called this way:
class ItemEnInventarioViewSet(InventarioListModelMixin, viewsets.ModelViewSet):
serializer_class = InventarioSerializer
renderer_classes = (UnicodeJSONRenderer,)
and my ListModelMixin is this:
class InventarioListModelMixin(object):
def list(self, request, *args, **kwargs):
item = request.QUERY_PARAMS.get('item', None)
inventario = Inventario.objects.filter(producto__item = item)
if inventario.count() == 0:
return HttpResponse(u"El item %s no se encuentra en el inventario" % item,status=400)
self.object_list = inventario
# Switch between paginated or standard style responses
page = self.paginate_queryset(self.object_list)
if page is not None:
serializer = self.get_pagination_serializer(page)
else:
serializer = self.get_serializer(self.object_list, many=True) <<--THIS IS THE PROBLEM
return Response(serializer.data)
It works fine, but when I try to GET form the DB arround 1000 or more entries, the serializer makes it very very slow, arround 25 to 35 seconds.
The Query to the DB is very simple so the DB is not the problem at all.
If I serialize the queryset with this function "data = serializers.serialize('json', myQuerySet)
" it takes at most 3 seconds but i dont get the info as I want, that's why I use a Custom Serializer
Is there a fastest way to GET that quantity of values? Maybe with another Serializer? any idea?
**ANSWER Thanks to Kevin ** Changing the query to:
inventario = Inventario.objects.select_related('producto__item','ubicacion__nombre').filter(producto__item = item)
...makes the Serializer not to hit the database every result-row to retrieve the Foreign values.