Inventree Python API: How to list all part categories
See our previous post Inventree Python API: Minimal API connect example using YAML config for our method of creating the api
object using a YAML config file!
from inventree.part import PartCategory
all_categories = PartCategory.list(api)
# Dict of part categories by name
# (e.g. 'OpAmps')
part_categories_by_name = {
category["name"]: category
for category in all_categories
}
# Dict of part categories by public key (e.g. 7)
part_categories_by_pk = {
category.pk: category
for category in all_categories
}
# Dict of part categories by hierarchical path
# (e.g. 'Electronics-Components/ICs/OpAmps')
part_categories_by_pathstring = {
category.pathstring: category
for category in all_categories
}
Note that this code by itself does not take into account the hierarchy of the categories.
Example output
part_categories_by_name
:
{'Elektronik-Komponenten': <inventree.part.PartCategory at 0x7fd735125510>,
'ICs': <inventree.part.PartCategory at 0x7fd7356d1720>,
'OpAmps': <inventree.part.PartCategory at 0x7fd735ead4e0>}
part_categories_by_pk
:
{1: <inventree.part.PartCategory at 0x7fd7346b5db0>,
2: <inventree.part.PartCategory at 0x7fd73438aa10>,
3: <inventree.part.PartCategory at 0x7fd727325b40>}
part_categories_by_pathstring
:
{'Elektronik-Komponenten': <inventree.part.PartCategory at 0x7fd727bd3040>,
'Elektronik-Komponenten/ICs': <inventree.part.PartCategory at 0x7fd727bd1db0>,
'Elektronik-Komponenten/ICs/OpAmps': <inventree.part.PartCategory at 0x7fd727bd1060>}