mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Defined custom SupplierPart object manager and prefetch related models in all queries
This commit is contained in:
		| @@ -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): | ||||
|  | ||||
|   | ||||
| @@ -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') | ||||
|   | ||||
		Reference in New Issue
	
	Block a user