Inventree Python API:如何创建新零件
有关我们使用 YAML 配置文件创建 api 对象的方法,请参见我们之前的文章 Inventree Python API:使用 YAML 配置的最小 API 连接示例!
以下代码查找名为 ICs 的零件类别并在该类别中创建新零件。
首先,我们需要获取零件类别,这来自我们之前的文章 Inventree Python API:使用 YAML 配置的最小 API 连接示例:
inventree_part_categories.py
from inventree.part import PartCategory
all_categories = PartCategory.list(api)
# 按名称分类的零件类别字典
# (例如 'OpAmps')
part_categories_by_name = {
category["name"]: category
for category in all_categories
}
# 按公钥分类的零件类别字典(例如 7)
part_categories_by_pk = {
category.pk: category
for category in all_categories
}
# 按层次路径分类的零件类别字典
# (例如 'Electronics-Components/ICs/OpAmps')
part_categories_by_pathstring = {
category.pathstring: category
for category in all_categories
}现在让我们选择正确的类别:
select_category.py
ics = part_categories_by_name['ICs']现在终于到了创建零件的时候:
inventree_create_part.py
from inventree.part import Part
new_part = Part.create(api, {
'name': 'L78L33ABD',
'description': '100mA 3.3V fixed LDO regulator, SOIC-8',
'category': ics.pk
})你可以使用以下命令读取新生成零件的主键(pk)
inventree_new_part_pk.py
new_part.pkIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow