mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-29 20:30:39 +00:00 
			
		
		
		
	Add manager class for StockItem
This commit is contained in:
		| @@ -84,7 +84,6 @@ class StockDetail(generics.RetrieveUpdateDestroyAPIView): | ||||
|     def get_queryset(self, *args, **kwargs): | ||||
|  | ||||
|         queryset = super().get_queryset(*args, **kwargs) | ||||
|         queryset = StockItemSerializer.prefetch_queryset(queryset) | ||||
|         queryset = StockItemSerializer.annotate_queryset(queryset) | ||||
|  | ||||
|         return queryset | ||||
| @@ -637,7 +636,6 @@ class StockList(generics.ListCreateAPIView): | ||||
|  | ||||
|         queryset = super().get_queryset(*args, **kwargs) | ||||
|  | ||||
|         queryset = StockItemSerializer.prefetch_queryset(queryset) | ||||
|         queryset = StockItemSerializer.annotate_queryset(queryset) | ||||
|  | ||||
|         return queryset | ||||
|   | ||||
| @@ -23,6 +23,7 @@ from django.dispatch import receiver | ||||
| from markdownx.models import MarkdownxField | ||||
|  | ||||
| from mptt.models import MPTTModel, TreeForeignKey | ||||
| from mptt.managers import TreeManager | ||||
|  | ||||
| from decimal import Decimal, InvalidOperation | ||||
| from datetime import datetime, timedelta | ||||
| @@ -130,6 +131,31 @@ def before_delete_stock_location(sender, instance, using, **kwargs): | ||||
|         child.save() | ||||
|  | ||||
|  | ||||
| class StockItemManager(TreeManager): | ||||
|     """ | ||||
|     Custom database manager for the StockItem class. | ||||
|  | ||||
|     StockItem querysets will automatically prefetch related fields. | ||||
|     """ | ||||
|  | ||||
|     def get_queryset(self): | ||||
|  | ||||
|         return super().get_queryset().prefetch_related( | ||||
|             'belongs_to', | ||||
|             'build', | ||||
|             'customer', | ||||
|             'purchase_order', | ||||
|             'sales_order', | ||||
|             'supplier_part', | ||||
|             'supplier_part__supplier', | ||||
|             'allocations', | ||||
|             'sales_order_allocations', | ||||
|             'location', | ||||
|             'part', | ||||
|             'tracking_info' | ||||
|         ) | ||||
|  | ||||
|  | ||||
| class StockItem(MPTTModel): | ||||
|     """ | ||||
|     A StockItem object represents a quantity of physical instances of a part. | ||||
|   | ||||
| @@ -70,29 +70,6 @@ class StockItemSerializer(InvenTreeModelSerializer): | ||||
|     - Includes serialization for the item location | ||||
|     """ | ||||
|  | ||||
|     @staticmethod | ||||
|     def prefetch_queryset(queryset): | ||||
|         """ | ||||
|         Prefetch related database tables, | ||||
|         to reduce database hits. | ||||
|         """ | ||||
|  | ||||
|         return queryset.prefetch_related( | ||||
|             'belongs_to', | ||||
|             'build', | ||||
|             'customer', | ||||
|             'purchase_order', | ||||
|             'sales_order', | ||||
|             'supplier_part', | ||||
|             'supplier_part__supplier', | ||||
|             'supplier_part__manufacturer_part__manufacturer', | ||||
|             'allocations', | ||||
|             'sales_order_allocations', | ||||
|             'location', | ||||
|             'part', | ||||
|             'tracking_info', | ||||
|         ) | ||||
|  | ||||
|     @staticmethod | ||||
|     def annotate_queryset(queryset): | ||||
|         """ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user