2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-21 06:16:29 +00:00

Add option to show part quantity in various forms

Enabling this option can make BOM item forms *very* slow!
This commit is contained in:
Oliver Walters
2021-01-14 15:20:42 +11:00
parent 8d6b4a2fd3
commit 1dc2636e45
4 changed files with 26 additions and 2 deletions

View File

@ -13,6 +13,8 @@ from mptt.fields import TreeNodeChoiceField
from django import forms
from django.utils.translation import ugettext as _
import common.models
from .models import Part, PartCategory, PartAttachment, PartRelated
from .models import BomItem
from .models import PartParameterTemplate, PartParameter
@ -23,8 +25,16 @@ from .models import PartSellPriceBreak
class PartModelChoiceField(forms.ModelChoiceField):
""" Extending string representation of Part instance with available stock """
def label_from_instance(self, part):
return f'{part} - {part.available_stock}'
label = str(part)
# Optionally display available part quantity
if common.models.InvenTreeSetting.get_setting('PART_SHOW_QUANTITY_IN_FORMS'):
label += f" - {part.available_stock}"
return label
class PartImageForm(HelperForm):

View File

@ -1990,7 +1990,13 @@ class BomItem(models.Model):
Return the available stock items for the referenced sub_part
"""
query = self.sub_part.stock_items.filter(StockModels.StockItem.IN_STOCK_FILTER).aggregate(
query = self.sub_part.stock_items.all()
query = query.prefetch_related([
'sub_part__stock_items',
])
query = query.filter(StockModels.StockItem.IN_STOCK_FILTER).aggregate(
available=Coalesce(Sum('quantity'), 0)
)