mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Merge pull request #355 from SchrodingersGat/speed2
Don't serialize 'available_stock' in PartSerializer
This commit is contained in:
		@@ -75,7 +75,14 @@ class SupplierPartList(generics.ListCreateAPIView):
 | 
				
			|||||||
    - POST: Create a new SupplierPart object
 | 
					    - POST: Create a new SupplierPart object
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    queryset = SupplierPart.objects.all()
 | 
					    queryset = SupplierPart.objects.all().prefetch_related(
 | 
				
			||||||
 | 
					        'part',
 | 
				
			||||||
 | 
					        'part__category',
 | 
				
			||||||
 | 
					        'part__stock_items',
 | 
				
			||||||
 | 
					        'part__bom_items',
 | 
				
			||||||
 | 
					        'part__builds',
 | 
				
			||||||
 | 
					        'supplier')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    serializer_class = SupplierPartSerializer
 | 
					    serializer_class = SupplierPartSerializer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    permission_classes = [
 | 
					    permission_classes = [
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,6 +17,7 @@ from django.conf import settings
 | 
				
			|||||||
from django.core.files.base import ContentFile
 | 
					from django.core.files.base import ContentFile
 | 
				
			||||||
from django.db import models, transaction
 | 
					from django.db import models, transaction
 | 
				
			||||||
from django.db.models import Sum
 | 
					from django.db.models import Sum
 | 
				
			||||||
 | 
					from django.db.models import prefetch_related_objects
 | 
				
			||||||
from django.core.validators import MinValueValidator
 | 
					from django.core.validators import MinValueValidator
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from django.contrib.staticfiles.templatetags.staticfiles import static
 | 
					from django.contrib.staticfiles.templatetags.staticfiles import static
 | 
				
			||||||
@@ -411,7 +412,7 @@ class Part(models.Model):
 | 
				
			|||||||
        total = None
 | 
					        total = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Calculate the minimum number of parts that can be built using each sub-part
 | 
					        # Calculate the minimum number of parts that can be built using each sub-part
 | 
				
			||||||
        for item in self.bom_items.all().select_related('sub_part'):
 | 
					        for item in self.bom_items.all().prefetch_related('sub_part__stock_items'):
 | 
				
			||||||
            stock = item.sub_part.available_stock
 | 
					            stock = item.sub_part.available_stock
 | 
				
			||||||
            n = int(1.0 * stock / item.quantity)
 | 
					            n = int(1.0 * stock / item.quantity)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -449,9 +450,11 @@ class Part(models.Model):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        builds = []
 | 
					        builds = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for item in self.used_in.all().prefetch_related('part'):
 | 
					        for item in self.used_in.all().prefetch_related('part__builds'):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            for build in item.part.active_builds:
 | 
					            active = item.part.active_builds
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            for build in active:
 | 
				
			||||||
                b = {}
 | 
					                b = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                b['build'] = build
 | 
					                b['build'] = build
 | 
				
			||||||
@@ -459,6 +462,8 @@ class Part(models.Model):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                builds.append(b)
 | 
					                builds.append(b)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        prefetch_related_objects(builds, 'build_items')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return builds
 | 
					        return builds
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -89,7 +89,7 @@ class PartSerializer(serializers.ModelSerializer):
 | 
				
			|||||||
            'keywords',
 | 
					            'keywords',
 | 
				
			||||||
            'URL',
 | 
					            'URL',
 | 
				
			||||||
            'total_stock',
 | 
					            'total_stock',
 | 
				
			||||||
            'available_stock',
 | 
					            # 'available_stock',
 | 
				
			||||||
            'units',
 | 
					            'units',
 | 
				
			||||||
            'trackable',
 | 
					            'trackable',
 | 
				
			||||||
            'buildable',
 | 
					            'buildable',
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user