Include some extra calculated fields for Part export (readonly)

This commit is contained in:
Oliver Walters 2019-09-13 23:15:34 +10:00
parent 52eeffc2c4
commit 6a19e94feb

View File

@ -20,17 +20,28 @@ from company.models import SupplierPart
class PartResource(ModelResource):
""" Class for managing Part data import/export """
# Constuct some extra fields for export
# ForeignKey fields
category = Field(attribute='category', widget=widgets.ForeignKeyWidget(PartCategory))
default_location = Field(attribute='default_location', widget=widgets.ForeignKeyWidget(StockLocation))
default_supplirt = Field(attribute='default_supplier', widget=widgets.ForeignKeyWidget(SupplierPart))
default_supplier = Field(attribute='default_supplier', widget=widgets.ForeignKeyWidget(SupplierPart))
category_name = Field(attribute='category__name', readonly=True)
variant_of = Field(attribute='variant_of', widget=widgets.ForeignKeyWidget(Part))
# Extra calculated meta-data (readonly)
in_stock = Field(attribute='total_stock', readonly=True, widget=widgets.IntegerWidget())
on_order = Field(attribute='on_order', readonly=True, widget=widgets.IntegerWidget())
used_in = Field(attribute='used_in_count', readonly=True, widget=widgets.IntegerWidget())
allocated = Field(attribute='allocation_count', readonly=True, widget=widgets.IntegerWidget())
building = Field(attribute='quantity_being_built', readonly=True, widget=widgets.IntegerWidget())
class Meta:
model = Part
skip_unchanged = True
@ -39,6 +50,20 @@ class PartResource(ModelResource):
'bom_checksum', 'bom_checked_by', 'bom_checked_date'
]
def get_queryset(self):
""" Prefetch related data for quicker access """
query = super().get_queryset()
query = query.prefetch_related(
'category',
'used_in',
'builds',
'supplier_parts__purchase_order_line_items',
'stock_items__allocations'
)
return query
class PartAdmin(ImportExportModelAdmin):