mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 12:06:44 +00:00
Include some extra calculated fields for Part export (readonly)
This commit is contained in:
parent
52eeffc2c4
commit
6a19e94feb
@ -20,17 +20,28 @@ from company.models import SupplierPart
|
|||||||
class PartResource(ModelResource):
|
class PartResource(ModelResource):
|
||||||
""" Class for managing Part data import/export """
|
""" Class for managing Part data import/export """
|
||||||
|
|
||||||
# Constuct some extra fields for export
|
# ForeignKey fields
|
||||||
category = Field(attribute='category', widget=widgets.ForeignKeyWidget(PartCategory))
|
category = Field(attribute='category', widget=widgets.ForeignKeyWidget(PartCategory))
|
||||||
|
|
||||||
default_location = Field(attribute='default_location', widget=widgets.ForeignKeyWidget(StockLocation))
|
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)
|
category_name = Field(attribute='category__name', readonly=True)
|
||||||
|
|
||||||
variant_of = Field(attribute='variant_of', widget=widgets.ForeignKeyWidget(Part))
|
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:
|
class Meta:
|
||||||
model = Part
|
model = Part
|
||||||
skip_unchanged = True
|
skip_unchanged = True
|
||||||
@ -39,6 +50,20 @@ class PartResource(ModelResource):
|
|||||||
'bom_checksum', 'bom_checked_by', 'bom_checked_date'
|
'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):
|
class PartAdmin(ImportExportModelAdmin):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user