Merge pull request #1812 from eeintech/improve_supplier_part_fetch

Custom SupplierPart object manager and prefetch related models
This commit is contained in:
Oliver 2021-07-14 07:59:25 +10:00 committed by GitHub
commit c816afbe62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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):