Defined custom SupplierPart object manager and prefetch related models in all queries

This commit is contained in:
eeintech 2021-07-13 12:55:36 -04:00
parent 2295008944
commit 3d9ad24e27
3 changed files with 20 additions and 5 deletions

View File

@ -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):

View File

@ -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')

View File

@ -15,6 +15,7 @@ from .models import SalesOrderAllocation
class PurchaseOrderLineItemInlineAdmin(admin.StackedInline):
model = PurchaseOrderLineItem
extra = 0
class PurchaseOrderAdmin(ImportExportModelAdmin):