API Python InvenTree : Comment lister toutes les catégories de pièces

Voir notre post précédent API Python InvenTree : Exemple minimal de connexion API en utilisant un fichier de configuration YAML pour notre méthode de création de l’objet api en utilisant un fichier de configuration YAML !

inventree_list_part_categories.py
from inventree.part import PartCategory

all_categories = PartCategory.list(api)

# Dictionnaire des catégories de pièces par nom
# (e.g. 'OpAmps')
part_categories_by_name = {
    category["name"]: category
    for category in all_categories
}
# Dictionnaire des catégories de pièces par clé publique (par ex. 7)
part_categories_by_pk = {
    category.pk: category
    for category in all_categories
}
# Dictionnaire des catégories de pièces par chemin hiérarchique
# (e.g. 'Electronics-Composants/ICs/OpAmps')
part_categories_by_pathstring = {
    category.pathstring: category
    for category in all_categories
}

Notez que ce code par lui-même ne prend pas en compte la hiérarchie des catégories.

Exemple de sortie

part_categories_by_name:

inventree_part_categories_by_name.py
{'Elektronik-Komponenten': <inventree.part.PartCategory at 0x7fd735125510>,
 'ICs': <inventree.part.PartCategory at 0x7fd7356d1720>,
 'OpAmps': <inventree.part.PartCategory at 0x7fd735ead4e0>}

part_categories_by_pk:

inventree_part_categories_by_pk.py
{1: <inventree.part.PartCategory at 0x7fd7346b5db0>,
 2: <inventree.part.PartCategory at 0x7fd73438aa10>,
 3: <inventree.part.PartCategory at 0x7fd727325b40>}

part_categories_by_pathstring:

inventree_part_categories_by_pathstring.txt
{'Elektronik-Komponenten': <inventree.part.PartCategory at 0x7fd727bd3040>,
 'Elektronik-Komponenten/ICs': <inventree.part.PartCategory at 0x7fd727bd1db0>,
 'Elektronik-Komponenten/ICs/OpAmps': <inventree.part.PartCategory at 0x7fd727bd1060>}

Consultez les articles similaires par catégorie : InvenTree Python