diff --git a/InvenTree/InvenTree/middleware.py b/InvenTree/InvenTree/middleware.py index 1b8320c060..bf4e5e990b 100644 --- a/InvenTree/InvenTree/middleware.py +++ b/InvenTree/InvenTree/middleware.py @@ -3,6 +3,7 @@ from django.urls import reverse_lazy from django.db import connection import logging import time +import operator logger = logging.getLogger(__name__) @@ -55,9 +56,18 @@ class QueryCountMiddleware(object): if len(connection.queries) > 0: + queries = {} + for query in connection.queries: query_time = query.get('time') + sql = query.get('sql').split('.')[0] + + if sql in queries: + queries[sql] += 1 + else: + queries[sql] = 1 + if query_time is None: # django-debug-toolbar monkeypatches the connection # cursor wrapper and adds extra information in each @@ -73,4 +83,7 @@ class QueryCountMiddleware(object): a=total_time, b=(t_stop - t_start))) + for x in sorted(queries.items(), key=operator.itemgetter(1), reverse=True): + print(x[0], ':', x[1]) + return response diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index d8da916cb2..6566ba52ad 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -34,6 +34,14 @@ class PartBriefSerializer(serializers.ModelSerializer): url = serializers.CharField(source='get_absolute_url', read_only=True) image_url = serializers.CharField(source='get_image_url', read_only=True) + + @staticmethod + def setup_eager_loading(queryset): + queryset = queryset.prefetch_related('category') + queryset = queryset.prefetch_related('stock_items') + queryset = queryset.prefetch_related('bom_items') + queryset = queryset.prefetch_related('builds') + return queryset class Meta: model = Part @@ -60,6 +68,8 @@ class PartSerializer(serializers.ModelSerializer): def setup_eager_loading(queryset): queryset = queryset.prefetch_related('category') queryset = queryset.prefetch_related('stock_items') + queryset = queryset.prefetch_related('bom_items') + queryset = queryset.prefetch_related('builds') return queryset class Meta: