More hungry fetching

This commit is contained in:
Oliver Walters 2019-05-21 00:16:00 +10:00
parent 157919f47a
commit 72aba30e81
3 changed files with 7 additions and 4 deletions

View File

@ -139,7 +139,7 @@ class Build(models.Model):
allocations = [] allocations = []
for item in self.part.bom_items.all(): for item in self.part.bom_items.all().prefetch_related('sub_part'):
# How many parts required for this build? # How many parts required for this build?
q_required = item.quantity * self.quantity q_required = item.quantity * self.quantity
@ -216,7 +216,7 @@ class Build(models.Model):
- Delete pending BuildItem objects - Delete pending BuildItem objects
""" """
for item in self.allocated_stock.all(): for item in self.allocated_stock.all().prefetch_related('stock_item'):
# Subtract stock from the item # Subtract stock from the item
item.stock_item.take_stock( item.stock_item.take_stock(
@ -295,7 +295,7 @@ class Build(models.Model):
""" Returns a dict of parts required to build this part (BOM) """ """ Returns a dict of parts required to build this part (BOM) """
parts = [] parts = []
for item in self.part.bom_items.all(): for item in self.part.bom_items.all().prefetch_related('sub_part'):
part = {'part': item.sub_part, part = {'part': item.sub_part,
'per_build': item.quantity, 'per_build': item.quantity,
'quantity': item.quantity * self.quantity, 'quantity': item.quantity * self.quantity,

View File

@ -532,7 +532,7 @@ class Part(models.Model):
hash = hashlib.md5(str(self.id).encode()) hash = hashlib.md5(str(self.id).encode())
for item in self.bom_items.all().prefetch('sub_part'): for item in self.bom_items.all().prefetch_related('sub_part'):
hash.update(str(item.sub_part.id).encode()) hash.update(str(item.sub_part.id).encode())
hash.update(str(item.sub_part.full_name).encode()) hash.update(str(item.sub_part.full_name).encode())
hash.update(str(item.quantity).encode()) hash.update(str(item.quantity).encode())

View File

@ -125,6 +125,9 @@ class BomItemSerializer(InvenTreeModelSerializer):
@staticmethod @staticmethod
def setup_eager_loading(queryset): def setup_eager_loading(queryset):
queryset = queryset.prefetch_related('part')
queryset = queryset.prefetch_related('part__category')
queryset = queryset.prefetch_related('part__stock_items')
queryset = queryset.prefetch_related('sub_part') queryset = queryset.prefetch_related('sub_part')
queryset = queryset.prefetch_related('sub_part__category') queryset = queryset.prefetch_related('sub_part__category')
queryset = queryset.prefetch_related('sub_part__stock_items') queryset = queryset.prefetch_related('sub_part__stock_items')