diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index aa065e9720..af0e458f47 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -110,6 +110,9 @@ class PartList(generics.ListCreateAPIView): except PartCategory.DoesNotExist: pass + # Ensure that related models are pre-loaded to reduce DB trips + parts_list = self.get_serializer_class().setup_eager_loading(parts_list) + return parts_list permission_classes = [ diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 593f7003f9..0e0c708156 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -58,6 +58,11 @@ class PartSerializer(serializers.ModelSerializer): image_url = serializers.CharField(source='get_image_url', read_only=True) category_name = serializers.CharField(source='category_path', read_only=True) + @staticmethod + def setup_eager_loading(queryset): + queryset = queryset.prefetch_related('category') + return queryset + class Meta: model = Part partial = True