From 3d9ad24e27621c97235a1a999092422ee0513c5b Mon Sep 17 00:00:00 2001 From: eeintech Date: Tue, 13 Jul 2021 12:55:36 -0400 Subject: [PATCH] Defined custom SupplierPart object manager and prefetch related models in all queries --- InvenTree/company/api.py | 6 +----- InvenTree/company/models.py | 18 ++++++++++++++++++ InvenTree/order/admin.py | 1 + 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 2c05c98d91..76b08ec27e 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -263,11 +263,7 @@ class SupplierPartList(generics.ListCreateAPIView): - POST: Create a new SupplierPart object """ - queryset = SupplierPart.objects.all().prefetch_related( - 'part', - 'supplier', - 'manufacturer_part__manufacturer', - ) + queryset = SupplierPart.objects.all() def get_queryset(self): diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 3786531311..778e00cde1 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -430,6 +430,22 @@ class ManufacturerPartParameter(models.Model): ) +class SupplierPartManager(models.Manager): + """ Define custom SupplierPart objects manager + + The main purpose of this manager is to improve database hit as the + SupplierPart model involves A LOT of foreign keys lookups + """ + + def get_queryset(self): + # Always prefetch related models + return super().get_queryset().prefetch_related( + 'part', + 'supplier', + 'manufacturer_part__manufacturer', + ) + + class SupplierPart(models.Model): """ Represents a unique part as provided by a Supplier Each SupplierPart is identified by a SKU (Supplier Part Number) @@ -450,6 +466,8 @@ class SupplierPart(models.Model): packaging: packaging that the part is supplied in, e.g. "Reel" """ + objects = SupplierPartManager() + @staticmethod def get_api_url(): return reverse('api-supplier-part-list') diff --git a/InvenTree/order/admin.py b/InvenTree/order/admin.py index 1e7b20e5a1..5d843fc624 100644 --- a/InvenTree/order/admin.py +++ b/InvenTree/order/admin.py @@ -15,6 +15,7 @@ from .models import SalesOrderAllocation class PurchaseOrderLineItemInlineAdmin(admin.StackedInline): model = PurchaseOrderLineItem + extra = 0 class PurchaseOrderAdmin(ImportExportModelAdmin):