You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been using the InvenTree API to generate BOMs in a custom XLSX format. It seems the InvenTree web does not allow me to export specific fields from the BOM to XLSX and it seems easier to use the API directly. Then I can also format the text (color etc.)
These are the steps I have used so far:
Iterate over all Parts on the server.
For each Part, get the BOM associated to it (part.getBomItems()). Select Parts that has a valid BOM assigned to it.
api=InvenTreeAPI(SERVER_ADDRESS, username=MY_USERNAME, password=MY_PASSWORD)
parts=Part.list(api)
row=0forpartinparts:
try:
bom_list=part.getBomItems()
except:
continue# Skip items with no BOMiflen(bom_list) ==0:
continue
Sort the BOM list is sorted in "assembly" parts and "non-assembly" parts. This allows nicer sorting so assemblies are always processed last when printed in the XLSX sheet. This might not be the smartest way to sort the list, but it works
# Sort BOM part types (i.e. individual parts, followed by sub-assemblies)# Note: Looking up parts from BomItems is kinda slow...But it worksbom_list_main= []
bom_list_subs= []
bom_list_sorted= []
forbom_iteminbom_list:
try:
sub_part=Part(api, pk=bom_item.sub_part)
ifsub_part.assembly:
bom_list_subs.append(bom_item)
else:
bom_list_main.append(bom_item)
except:
continuebom_list_sorted.extend(bom_list_main)
bom_list_sorted.extend(bom_list_subs)
Iterate over all BOM Parts. Calculate required quantity. Print the relevant fields to the XLSX worksheet.
forbom_iteminbom_list_sorted:
try:
# Lookup selected BOM partpart=Part(api, pk=bom_item.sub_part)
# Calculate build quantity, assuming current stock (round to nearest)can_build=bom_item.available_stock/ (bom_item.quantity*bom_multiplier) ifbom_item.quantityelse0ifpart.assembly:
# Handle nested BOMs, i.e. when BOM part is an assembly:# Sort nested BOM items again# Handle nested BOM and print fields to XLSX
...
else:
# Handle individual BOM items:
...
Some BOMs contain nested BOMs so these are handled by repeating step 3-5 recursively.
The above steps handle all situations, except substitute parts, so I am now trying to handle that.
For each bom_item , i get the list of substitutes (bom_item.substitutes). This list does not contain a list of Parts as expected, but a list with a dict for each substitute part. I convert this to a Part by the following dict look-up:
part = Part(api, pk=substitute['part_detail']['pk'])
I was expecting to be able to access substitute.pk etc. directly. Is there something I am missing?
When I handle nested BOMs, I keep track of the BOM level/depth by hand to allow easier sorting of the components. Is there a different way to get hold of the BOM "depth" index directly using the API? Otherwise, I have to keep track of the BOM "depth" each time i detect sub_part.assembly
Is there a way to obtain a sorted BOM list from the API? Eg. if I want to sort by Assemblies or items with a stock quantity of 0?
On an unrelated note. I was wondering if these scripts I have been working on can be useful in any way - perhaps as an "example" of how to use the API. This API has been most useful, so it would be nice to give back to the project somehow. :)
The text was updated successfully, but these errors were encountered:
On an unrelated note. I was wondering if these scripts I have been working on can be useful in any way - perhaps as an "example" of how to use the API.
Certainly you could add something like this to our "examples" page:
Hello,
I have been using the InvenTree API to generate BOMs in a custom XLSX format. It seems the InvenTree web does not allow me to export specific fields from the BOM to XLSX and it seems easier to use the API directly. Then I can also format the text (color etc.)
These are the steps I have used so far:
The above steps handle all situations, except substitute parts, so I am now trying to handle that.
For each
bom_item
, i get the list of substitutes (bom_item.substitutes
). This list does not contain a list of Parts as expected, but a list with a dict for each substitute part. I convert this to a Part by the following dict look-up:substitute.pk
etc. directly. Is there something I am missing?sub_part.assembly
On an unrelated note. I was wondering if these scripts I have been working on can be useful in any way - perhaps as an "example" of how to use the API. This API has been most useful, so it would be nice to give back to the project somehow. :)
The text was updated successfully, but these errors were encountered: